eclipse - Exception in thread "main" java.lang.NullPointerException in DVD sorting program -


i attempting create program puts dvd information array , sorts them alphabetically based on title using compareto method comparable.

i have boolean method finds out whether 2 titles same:

public boolean equals (object other) {     return (title.equals(((dvd)other).gettitle())); } 

and compareto method looks this:

public int compareto (object other) {     int result;      string othertitle = ((dvd)other).gettitle();      result = title.compareto(othertitle);      return result; } 

these 2 methods in dvd class have implementing comparable.

the sorting algorithm took used directly example sorts names alphabetically here well:

public class dvdsorting  {     public static void selectionsort (comparable[] list)     {         int min;         comparable temp;          (int index = 0; index < list.length-1; index++)         {             min = index;             (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)     {         (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;         }     } } 

i made dvdcollection class initializes array , contains method add dvd's.

in driver class test able insert dvd's , print out has been inserted when attempt sort them null pointer exception though don't see initialized null. here driver:

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 eve", "joseph mankiewicz", 1950, 17.50, false);         movies.adddvd("the matrix", "andy & lana wachowski", 1999, 19.95, true);         movies.adddvd("clash of 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 titans", "desmond davis", 1981, 5.00, false);           dvdsorting.selectionsort(movies.collection);         system.out.println (movies);     } } 

i used movies.collection in sorting call because when attempted use movies examples have seen, said selectionsort not applicable movies made collection public variable , used that.

here's error.

exception in thread "main" java.lang.nullpointerexception     @ dvdsorting.selectionsort(dvdsorting.java:13)     @ movies.main(movies.java:15) 

it tells me error in dvdsorting class since that's copied source have feeling actual error in compareto method in dvd class.

i'm sorry if question doesn't follow proper etiquette of site, it's first time posting. appreciated.

my guess either list or list[scan] null @ point in selectionsort() method. perhaps try verifying movies.collection (from main method) storing values think is.

this task eclipse debugger.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -