NullPointerException thrown?
Im trying to have an Object implement a custom Comparable but the single compareTo(T o) method found in the Comparable interface has to stay like it is. ie Having an object as a parameter. So i added in the cast to the type which is being held in the Collection and thus being sorted upon. The line i added It sounds correct since a ClassCastException should never be thrown since the Collection is marked to hold only employee objects. Any ideas? Thanks.
Code:
public int compareTo(Object o){
Employee e = (Employee)o; // added line
int i = lname.compareTo(e.lname);
return (i != 0 ? i : fname.compareTo(e.fname));
}
Re: NullPointerException thrown?
I forgot the Comparable interface is.
Code:
public interface Comparable<T>{
public int compareTo(T o);
}
I marked the interface that the class implements as implements Comparable<Employee>. Now i can use an Employee type as a parameter instead of an Object. But a NullPointerException is still being thrown. :confused:
Re: NullPointerException thrown?
I figured it out. I had non initialized Strings so the NullPointerException was being thrown after attempting to invoke compareTo(String s). :blush: