Sheriff
Sheriff

Reputation: 495

Class Declaration causes Null Pointer Exception

My program crashes due to a null pointer exception.

Snip of Stack Trace:

java.lang.NullPointerException
at com.my.GRTRecTimeSortComparator.compare(SourceFile:15)

That's well and good. But my problem is that line 15 is the class declaration:

15 public class GRTRecTimeSortComparator   implements Comparator<GRTRecord> {
16    @Override
17    public int compare(GRTRecord rec1, GRTRecord rec2) {
18      
19      int returnVal=rec1.getRecordCalendar().compareTo(rec2.getRecordCalendar());
20        return returnVal;
21    }
22}

It's calling out the Compare function, but I don't see an issue with that either. Every record has a Unique Calendar assigned to it as well.

Any ideas? What have I done wrong here?

Edit: in response to queries:

No, there is nothing strange about the class itself, other than the fact I wrote it.

I AM using Proguard. Maybe the Obfuscation is replacing things incorrectly?

I'm writing up tests for the records right now. They SHOULDN'T be null, nor should the Calendars; I use them immediately before, and get good results.

Maybe I'm just passing the ArrayList incorrectly.

EDIT 2:

Logging lines inserted into the class never get run. No matter what I add or do to the class, the null pointer always resolves to the class declaration line.

Removed Proguard Obfuscation, and still no dice.

I'm REALLY frustrated.

Edit 3:

For Future Users:

Proguard ruins your stack trace. That was the cause of the Above. Remove proguard if you run into issues.

The CAUSE was actually a corrupted record in the dataset.

Upvotes: 0

Views: 640

Answers (5)

Tushar Paliwal
Tushar Paliwal

Reputation: 301

If you are implementing comparator interface then you can use only compare() method but you are trying to access compareTo() method of comparable which you didn't implement. In your program:

The comparing value is returned in the form of reference form which could not be return to a primitive type.

Upvotes: 0

Eric Lafortune
Eric Lafortune

Reputation: 45678

ProGuard tries to preserve debugging information as much as possible, but that is not always technically possible. For instance, if the method getRecordCalendar gets inlined, any exceptions that the method throws are now thrown from the invocation site.

Upvotes: 0

Mike
Mike

Reputation: 3311

You might want to try checking for a null rec1 and rec2 for completeness

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502106

I strongly suspect that the debug information is just out of sync. Put logging into your compare method. I would log (in order):

  • rec1
  • rec2
  • rec1.getRecordCalendar()
  • rec2.getRecordCalendar()

If this isn't the problem, is there anything "odd" about the class in any other way? For example:

  • Are you using Hibernate or something similar which may be creating proxies for you?
  • Is this an inner class?

Upvotes: 1

hvgotcodes
hvgotcodes

Reputation: 120258

At times like these, its good to set your debugger to break on error. You will know exactly where your NPE is.

Upvotes: 1

Related Questions