简体   繁体   中英

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! Nothing 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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM