views:

191

answers:

3

Why does this throw NPE

public static void main(String[] args) throws Exception {
    Boolean b = true ? returnsNull() : false; // NPE on this line.
    System.out.println(b);
}

public static Boolean returnsNull() {
    return null;
}

while this doesn't

public static void main(String[] args) throws Exception {
    Boolean b = true ? null : false;
    System.out.println(b); // null
}

?

The solution is by the way to replace false by Boolean.FALSE to avoid null being unboxed to boolean --which isn't possible. But that isn't the question. The question is why? Are there any references in JLS which confirms this behaviour, especially of the 2nd case?

+1  A: 

The line:

    Boolean b = true ? returnsNull() : false;

is internally transformed to:

    Boolean b = true ? returnsNull().getBoolean() : false; 

to perform the unboxing; thus: null.getBoolean() will yield a NPE

This is one of the major pitfalls when using autoboxing. This behavious is indeed documented in 5.1.8 JLS

Edit: I believe the unboxing is due to the third operator being of boolean type, like (implicit cast added):

   Boolean b = (Boolean) true ? true : false; 
jjungnickel
Why does it try to unbox like that, when the ultimate value is a Boolean object?
Erick Robertson
+6  A: 

From Java Language Specification, section 15.25:

  • If one of the second and third operands is of type boolean and the type of the other is of type Boolean, then the type of the conditional expression is boolean.

So, the first example tries to call Boolean.booleanValue() in order to convert Boolean to boolean as per the first rule.

In the second case the first operand is of the null type, when the second is not of the reference type, so autoboxing conversion is applied:

  • Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2. The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T1, T2) (§15.12.2.7).
axtavt
This answers the first case, but not the second case.
BalusC
Probably there is an exception for when one of the values is `null`.
Erick Robertson
@Erick: does JLS confirm this?
BalusC
Yes. It's in the next line in the link @axtavt provided. I updated the answer.
Erick Robertson
@Erick: I don't think it's applicable since `boolean` is not a reference type.
axtavt
I stand corrected.
Erick Robertson
And may I add ... this is why you should make both sides of a ternary the same type, with explicit calls if necessary. Even if you have the specs memorized and know what will happen, the next programmer to come along and read your code might not. In my humble opinion, it would be better if the compiler just produced an error message in these situations rather than doing things that are difficult for ordinary mortals to predict. Well, maybe there are cases where the behavior is truly useful, but I haven't hit one yet.
Jay
I was so free to remove "the next line" in the JLS which Erick added but which was after all irrelevant.
BalusC
+2  A: 

The difference is static typing of the expressions at compile time:

E1: `true ? returnsNull() : false` - boolean (auto-unboxing 2nd op to boolean)

E2: `true ? null : false` - Boolean (autoboxing of 3rd param to Boolean)

See Java Language Specification, section 15.25 Conditional Operator ? :

  • For E1, this clause applies:

    If one of the second and third operands is of type boolean and the type of the other is of type Boolean, then the type of the conditional expression is boolean.

    The compiler inserts auto-unboxing code to the 2nd operand (return value of returnsNull()) to make it type boolean. This of course causes the NPE from the null returned at run-time.

  • For E2, no specific typing clause applies (go read 'em!), so the final "otherwise" clause applies:

    Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2. The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T1, T2) (§15.12.2.7).

    The compiler inserts auto-boxing code for the 3rd operand (false). The 2nd operand is already Boolean, so no auto-boxing NPE.


This question needs a similar type analysis:

http://stackoverflow.com/questions/2615498/java-conditional-operator-result-type

Bert F
Makes sense ... I think. The [§15.12.2.7](http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#341287) is a pain.
BalusC
It's easy ... but only in hindsight. :-)
Bert F