views:

90

answers:

1

Consider this class:

public class TestMap extends HashMap<String, Float> {

    public static void main(String[] args) {
        TestMap tm = new TestMap();
        tm.put("A", 0F);
        tm.put("B", null);

        String[] keys = new String[]{"A", "B"};

        for (String key : keys) {
            System.out.println(key);
            Float foo = (tm == null ? 0F : tm.get(key));
//          Float foo = tm.get(key);
            System.out.println(foo);
        }       
    }
}

A NullPointerException is produced on the line Float foo =... during the second iteration of the loop:

A
0.0
B
Exception in thread "main" java.lang.NullPointerException
    at TestMap.main(TestMap.java:14)

If I replace the existing line with the commented line immediately below it works as expected, assigning foo = null. Why is the behavior different in these two cases?

+15  A: 

The value for key "B" is null. But the return type of your conditional expression is a float (primitive) due to you using the literal 0F, not a Float (wrapper), so the value has to be autounboxed (to a primitive) and then autoboxed (back to an object). This results in the NPE.

Compare to what happens when you use

Float foo = (tm == null ? Float.valueOf(0f) : tm.get(key));

As an added explanation, here's what your conditional is doing (showing explicit boxing):

Float foo;
float conditionalResult;

if ( tm == null ) {
    conditionalResult = 0F;
} else {
    conditionalResult = tm.get(key).floatValue(); //this throws the NPE
}
foo = Float.valueOf(conditionalResult);

JLS 15.25 defines what the return type of a conditional operator will be. It's quite complex, and I find it easier to learn through experimentation :-).

Mark Peters
That's tricky - didn't expect the conditional to alter the return type of the second clause based on the first... thanks for the explanation.
Greg Harman
@Greg: Yes, that part can be confusing at first. Java needs to know statically (at compile time) what the return type is, so it needs to choose a single return type that works for both possibilities.
Mark Peters