3

Why does the following Java code throw a NullPointerException?

public static void main(String[] args) {
    getInteger(null);
}

public static Integer getInteger(Number n) {
    return (n == null || n instanceof Integer) ? (Integer)n : n.intValue();
}

EDIT: I added parentheses to end the confusion about "whether I am sometimes returning a boolean".

9
  • @TimBiegeleisen That's what I thought too. Test it yourself :) Dec 3, 2015 at 7:04
  • 1
    as a sidenode, n instanceof Integer will also fail if n is null, so you are basicly checking it twice. Dec 3, 2015 at 7:09
  • 3
    @HansBrende change n.intValue() to Integer.valueOf(n.intValue()) (all the current answers seem incorrect).
    – assylias
    Dec 3, 2015 at 7:25
  • 1
    @assylias thank you for the only correct response! Java auto-unboxing at work... shoulda known. Dec 3, 2015 at 7:30
  • 3
    @Tim, since the second part of the expression is n.intValue() which gives an primitive int, the compiler uses unboxing on the first part as well... So (Integer) n becomes ((Integer) n).intValue() in compiled code. So this throws a NPE on null input. Hope it clears.
    – Codebender
    Dec 3, 2015 at 7:34

2 Answers 2

4

Credit is completely due to @assylias who discovered the answer. Use this code instead:

public static Integer getInteger(Number n) {
    return (n == null || n instanceof Integer) ? (Integer)n : Integer.valueOf(n.intValue());
}

Because of weird boxing/unboxing rules, since n.intValue() returns a primitive int, the compiler was unboxing n in the expression (Integer)n to a primitive as well. And null cannot be assigned to a primitive (just try doing that in your IDE).

Edit:

The NPE is not really because null is being assigned to a primitive (the compiler would not do that), but it's because unboxing is done by calling Integer.intValue() method and the method is called on a null reference in this case.

11
  • 1
    But this doesn't tell me why my code failed. Dec 3, 2015 at 7:08
  • Yes it does. In the case of null, you were trying to return a boolean value in the RHS expression. Dec 3, 2015 at 7:09
  • 3
    @TimBiegeleisen don't you think that IDE should alert the user that this statement might return boolean? I tried the code and I was thinking that.. Dec 3, 2015 at 7:10
  • @DanyalSandeelo Very good question. Dec 3, 2015 at 7:11
  • 1
    @TimBiegeleisen the problem is that both are being UNboxed to int. Dec 3, 2015 at 7:34
0

Because here:

return n == null || n instanceof Integer ? (Integer)n : n.intValue();
                                             here

You are trying to convert the n (which is null) to Integer.

7
  • 1
    But null CAN be converted to an Integer. Dec 3, 2015 at 7:07
  • I tried the code, the check is false so the code is not going there, Dec 3, 2015 at 7:07
  • @KostasC all know because of casing time throw NullPointerException, but see n== null returns true then how moves to right side of short-circulate OR operator Dec 3, 2015 at 7:10
  • 1
    This answer is incorrect... See duplicate.
    – assylias
    Dec 3, 2015 at 7:38
  • 1
    @assylias Thanks for correcting us. Dec 3, 2015 at 9:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.