I have implemented a comparator class which sorts an array of objects, or "Persons", in my case. However, there seem to be a few bugs, so when I click the next button of my GUI, I get the following error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at RectangleProgram$CustomComparator.compare(RectangleProgram.java:30)
at RectangleProgram$CustomComparator.compare(RectangleProgram.java:26)
at java.util.TimSort.binarySort(TimSort.java:265)
at java.util.TimSort.sort(TimSort.java:208)
Here are snippets of the code that are relevant:
public class Person//HERE IS THE OBJECT THE COMPARATOR IS REFERRING TO
{
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 TO BE SORTED
// class implementing the sort
public class re implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if (counter==0)
{
getData();
Arrays.sort(arr, new CustomComparator());
}
}
}
Could someone tell me what is wrong with the code, and is causing the error? Note, it is not a compilation error, it is just that clicking the next button [of which class re is the action listener] does not make it do anything.