Results 1 to 3 of 3

Thread: NullPointerException thrown?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Resolved 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));  
    }

  2. #2

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    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.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    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).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width