开发者

NullPointerException when assigning numbers

开发者 https://www.devze.com 2023-04-02 21:47 出处:网络
There is a strange thing happening when I execute the following code: 开发者_JAVA百科private void doStuff(Long inLong) {

There is a strange thing happening when I execute the following code:

开发者_JAVA百科
private void doStuff(Long inLong) {
    long theNumber = inLong;

    /* ... */
}

Sometimes I see a NullPointerException in the logs at the assignment line and I can't understand why it happens. Any idea?


You passed in null. Check what you are passing in.


For long theNumber = inLong;, the long value of inLong is fetched by implicitly calling inLong.longValue(). This is called auto-unboxing (sometimes more general auto-boxing). When inLong is null, you therefore get a NullPointerException just like calling any other method on null.

Therefore, you should think of some alternative:

If you do not need an undefined value, you should make sure the caller never passes null and assert this via:

private void doStuff(Long inLong) {
  assert inLong != null; 
  long theNumber = inLong;
  /* ... */
} 

(or use a Nonnull-Checker). If the method were public and you cannot be sure that null is not passed, rather do:

public void doStuff(Long inLong) {
  if (inLong == null) { throw new IllegalArgumentException(); } 
  long theNumber = inLong;
  /* ... */
} 

If you neither need undefined values nor use the value in a collection (or in any other generic parameter), rather use the method private void doStuff(long inLong) instead. If you do need a Long object, you can, of course, still use the parameter of type long and do (auto-)boxing inside of doStuff to get an according Long object.

If you do need undefined values, you should check for it and do what is necessary:

private void doStuff(Long inLong) {
  if (inLong == null) { 
    handleUndef(); 
  } else {
    long theNumber = inLong;
    /* ... */
  }
} 

Just setting the value to 0 instead of some more elaborate handleUndef() is dubious in my opinion, because then you could have used the parameter of type long (see above) in the first place.


inLong is probably null. Then you try to assign a null reference and unbox it into a primitive type.


because inLong can be null and will not be automatically mapped to 0.
I guess what you want to do is this:

theNumber = 0; // or Long.MIN_VALUE or Long.MAX_VALUE or whatever you prefer
if (inLong != null) {
    theNumber = inLong;
}
// ... 


That's because the inLong parameter is null. Whenever you assign a Long object to a long variable, Java automatically tries to unbox it to the proper type, but it fails if value of the Long variable is null.

Just put a null-check before that assignment and you'll get rid of that error, but you may have to raise an exception or deal with the null argument appropriately.


if inLong is null, then NPE is the expected behaviour.

long theNumber = inLong;

is semantically equivalent to

long theNumber = inLong.longValue();

which should make the cause of the NPE obvious.

0

精彩评论

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

关注公众号