Getting unwanted NullPointerException in ternary operator - Why? [duplicate]

不想你离开。 提交于 2019-12-18 11:40:35

问题


While executing the following code, I am getting a NullPointerException at line:

value = condition ? getDouble() : 1.0;

In earlier lines when I use null instead of getDouble() everything works and this is strange.

public class Test {
    static Double getDouble() {
        return null;
    }

    public static void main(String[] args) {
        boolean condition = true;
        Double value;

        value = condition ? null : 1.0;         //works fine
        System.out.println(value);              //prints null

        value = condition ? getDouble() : 1.0;  //throws NPE
        System.out.println(value);
    }
}

Can someone help me understand this behavior?


回答1:


When you write

value = condition ? null : 1.0;

the type of condition ? null : 1.0 must be a reference type, so the type is Double, which can hold the value null.

When you write

value = condition ? getDouble() : 1.0;

and getDouble() returns null, it's equivalent to writing:

value = condition ? ((Double) null) : 1.0;

In this case the compiler sees a Double and a double as the 2nd and 3rd arguments of the ternary conditional operator, and decides that type of the expression should be double. Therefore it unboxes the null to double, getting NullPointerException.

The type of the conditional ternary operator is determined by some tables in JLS 15.25.

If the 2nd and 3rd operands are null and double, the conditional expression type is the least upper bound of Double and null, which is Double.

If the 2nd and 3rd operands are Double and double, the conditional expression type is double.




回答2:


See #jls-15.25:

If the 2nd operand is Double, while the 3rd operand is double, the result:

getCount() == 1 ? getDouble() : 1.0

will be a double.

And when you try to convert a Double null(returned by getDouble()) to double, NPE will be thrown.



来源:https://stackoverflow.com/questions/52147247/getting-unwanted-nullpointerexception-in-ternary-operator-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!