2013-04-05 56 views
0

我試圖創建一個使用compareTo方法把DVD的信息到一個數組中,並對其進行排序按字母順序基於標題可比的程序。異常線程「main」顯示java.lang.NullPointerException在DVD整理程序

我有一個發現了兩個冠軍是否相同的布爾方法:

public boolean equals (Object other) 
{ 
    return (title.equals(((DVD)other).getTitle())); 
} 

和我compareTo方法是這樣的:

public int compareTo (Object other) 
{ 
    int result; 

    String otherTitle = ((DVD)other).getTitle(); 

    result = title.compareTo(otherTitle); 

    return result; 
} 

這兩種方法都在DVD類我已經實現了Comparable。

排序算法本身我拉着直接從字母順序排列的名字一個例子使用,但這裏說的還有:

public class DVDSorting 
{ 
    public static void selectionSort (Comparable[] list) 
    { 
     int min; 
     Comparable temp; 

     for (int index = 0; index < list.length-1; index++) 
     { 
      min = index; 
      for (int scan = index+1; scan < list.length; scan++) 
       if (list[scan].compareTo(list[min]) < 0) 
        min = scan; 

      temp = list[min]; 
      list[min] = list[index]; 
      list[index] = temp; 
     } 
    } 

    public static void insertionSort(Comparable[] list) 
    { 
     for (int index = 1; index < list.length; index++) 
     { 
      Comparable key = list[index]; 
      int position = index; 
      while (position > 0 && key.compareTo(list[position-1]) < 0) 
      { 
       list[position] = list[position-1]; 
       position--; 
      } 

      list[position] = key; 
     } 
    } 
} 

我當時提出,初始化數組,幷包含一個方法來添加DVD的一個DVDCollection類。

在我的司機類來測試我能插入DVD和打印出的內容已經插入,但是當我試圖對它們進行排序,我得到即使我沒有看到任何初始化爲null空指針異常。這裏是我的司機:

public class Movies 
{ 
    public static void main (String[] args) 
    { 
     DVDCollection movies = new DVDCollection(); 

     movies.addDVD("The Godfather", "Francis Ford Coppola", 1972, 24.95, true); 
     movies.addDVD("District 9", "Neill Blomkamp", 2009, 19.95, false); 
     movies.addDVD("Iron Man", "Jon Favreau", 2008, 15.95, false); 
     movies.addDVD("All About Eve", "Joseph Mankiewicz", 1950, 17.50, false); 
     movies.addDVD("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true); 
     movies.addDVD("Clash of the Titans", "Louis Leterrier", 2010, 19.95, true); 

     DVDSorting.selectionSort(movies.collection); 
     System.out.println (movies); 

     movies.addDVD("Iron Man 2", "Jon Favreau", 2010, 22.99, false); 
     movies.addDVD("Casablanca", "Michael Curtiz", 1942, 19.95, false); 
     movies.addDVD("Clash of the Titans", "Desmond Davis", 1981, 5.00, false); 


     DVDSorting.selectionSort(movies.collection); 
     System.out.println (movies); 
    } 
} 

我以前在分揀通話movies.collection,因爲當我嘗試使用類似的例子只是電影我都看到了,它說選擇排序是不適用的電影所以我做了收集公共變量只是用它。

這是錯誤。

Exception in thread "main" java.lang.NullPointerException 
    at DVDSorting.selectionSort(DVDSorting.java:13) 
    at Movies.main(Movies.java:15) 

它告訴我的錯誤是在DVDSorting類,但因爲這是從其他來源複製我有一種感覺,實際的錯誤是在我的DVD類我compareTo方法。

我很抱歉,如果我的問題不遵循該網站的適當的禮儀,這是我第一次發佈。任何幫助將不勝感激。

回答

0

我的猜測是listlist[scan]selectionSort()方法中的某個點爲空。也許嘗試驗證movies.collection(來自您的主要方法)實際上是存儲您認爲它的值。

這是Eclipse調試器一個好工作。

+0

在選擇排序()方法唯一的例外是在我調用的compareTo()方法的行。我只是對如何工作有點困惑。我是否正確實施了這一點? – 2013-04-05 06:50:12

+0

如果'compareTo()'中發生異常,那麼該方法將列在異常堆棧跟蹤的頂部,位於'selectionSort()'的上方。但是由於您知道該行發生異常,因此'NullPointerException'的唯一可能性是'list [scan]'對於'scan'的某個值爲空。 – Octahedron 2013-04-05 13:10:47

相關問題