java - Bug in Comparator Class -


i have implemented comparator class sorts array of objects, or "persons", in case. however, there seem few bugs, when click next button of gui, following error:

exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ rectangleprogram$customcomparator.compare(rectangleprogram.java:30) @ rectangleprogram$customcomparator.compare(rectangleprogram.java:26) @ java.util.timsort.binarysort(timsort.java:265) @ java.util.timsort.sort(timsort.java:208) 

here snippets of code relevant:

public class person//here object comparator referring      {         string firstname; string lastname; int z;          public person(string l, string m, int e)         {            firstname=l; lastname=m; z=e;          }         public string getfirstname()         {            return firstname;         }      }  public class customcomparator implements comparator<person> { // comparator begins here       @override       public int compare(person object1, person object2)       {               return object1.getfirstname().compareto(object2.getfirstname());        } } // comparator ends here public static person [] arr=new person [100]; // array sorted // class implementing sort public class re implements actionlistener     {         public void actionperformed (actionevent e)          {             if (counter==0)            {               getdata();                arrays.sort(arr, new customcomparator());             }         }    } 

could tell me wrong code, , causing error? note, not compilation error, clicking next button [of class re action listener] not make anything.

i tried in java 8

code:

        list<person> list = new arraylist<>();         (int = 0; < 10; i++) {             person p = new person(integer.tostring(i), integer.tostring(i), i);             list.add(p);         }         collections.shuffle(list);         system.out.println("the shuffled list");         list.foreach(i -> system.out.println(i.tostring() + " "));         collections.sort(list, (p1, p2) -> p1.getfirstname().compareto(p2.getfirstname()));         system.out.println("------------------------ \n orderd list");         list.foreach(i -> system.out.println(i.tostring() + " ")); 

output:

the shuffled list 0 0 0  9 9 9  8 8 8  7 7 7  3 3 3  6 6 6  2 2 2  4 4 4  1 1 1  5 5 5  ------------------------   orderd list 0 0 0  1 1 1  2 2 2  3 3 3  4 4 4  5 5 5  6 6 6  7 7 7  8 8 8  9 9 9  

let me know happened or need explanation


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -