java - I cannot figure out why my collection is null at some points -
i adding dvds collection. , trying sort every time dvd added. keep getting error message:
exception in thread "main" java.lang.nullpointerexception @ dvdcollection.adddvd(dvdcollection.java:44)
could guys show me how fix it?
public void adddvd (string title, string director, int year, double cost, boolean bluray) { if (count == collection.length) increasesize(); collection[count] = new dvd (title, director, year, cost, bluray); totalcost += cost; count++; if (count > 0) { int min; (int = 0; < collection.length - 1; i++) { min = i; (int j = + 1; j < collection.length; j ++) { if(collection[j].getdirector().compareto(collection[i].getdirector()) < 0) min = j; temp[min] = collection[min]; collection[min] = collection[j]; collection[j] = temp[min]; } } } } public class movies { //----------------------------------------------------------------- // creates dvdcollection object , adds dvds it. prints // reports on status of 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); system.out.println (movies); movies.adddvd ("iron man 2", "jon favreau", 2010, 22.99, false); movies.adddvd ("casablanca", "michael curtiz", 1942, 19.95, false); system.out.println (movies); system.out.println(movies); } }
java comes lot of handy libraries. don't have reinvent wheel , sorting hand (except learning).
hence, make dvd implement comparable comparison based on director, , use treeset.
this set keeps elements sorted natural order, using tree structure : more efficient sorting collection @ each insertion.
Comments
Post a Comment