开发者

Can someone explain what this error means please?

java.lang.NullPointerException
    at java.lang.Integer.compareTo(Unknown Source)
    at java.lang.Integer.compareTo(Unknown Source)

I slightly edited my code, deleted no methods but changed the name of one or two, and now...boom! Nothi开发者_开发技巧ng works ! So annoying because I just had it working, went back and commented it and now I can't see whats changed...help ? :)


You're probably trying to do something like this:

Integer i = null;
Integer j = 42;
i.compareTo(j); // throws NullPointerException since i is null

or this:

Integer i = 21;
Integer j = null;
i.compareTo(j); // throws NullPointerException since j is null

but you haven't shown any code.


From the docs:

Thrown when an application attempts to use null in a case where an object is required. These include:

  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.

Applications should throw instances of this class to indicate other illegal uses of the null object.

You will surely have encountered one of these cases. On a more higher level, you might have called a function (here compareTo()), with a null argument, which then leads to a NullPointerException in the function.

Maybe you have a TreeMap, and inserted a null into the map?


Put a break point in you ide at that line and see what var is null. From there it will be a easy fix.


Are you sure that when you changed the name of your method that you changed it everywhere you called it? Chances are that with a null pointer exception, you have an improperly typed or spelled name. It looks like you were trying to compare two integers, and chances are that the method that gives out one of those integers isn't being called correctly anymore. Always make sure to double check.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜