问题描述
我喜欢在java编程中使用三元条件表达式,但是我遇到了一个问题:
下面的代码是一个小例子,但它显示了我发现的问题.
公共类示例{公共示例(){双 x = 0.0;A a = new A();x = a == 空?0.0 : a.getY();//发生在这一行System.out.println(x);}A类{双 y = 空;私人双 getY() {返回 y;}}公共静态无效主(字符串 [] args){新的例子();}}
是什么导致了 NullPointerException
?
发生这种情况是因为 0.0
是 double
类型,而不是 Double
类型.条件运算符的后两个操作数必须是相同类型,因此自动装箱/拆箱进入其中,编译器将该代码转换为:
x = Double.valueOf(a == null ? 0.0 : a.getY().doubleValue());//-^^^^^^^^^^^^^^^----------^^^^^^^^^^^^^^^
...抛出,因为 a.getY()
返回 null
,然后代码尝试在 上调用
.doubleValue
null
如果我们运行 javap -c Example
来反编译代码,我们可以看到那些调用(我把它们加粗了):
I like to use the ternary conditional expression in java programming, but I experienced an issue :
The following code is a small example, but it shows the problem that I have found.
public class Example {
public Example() {
Double x = 0.0;
A a = new A();
x = a == null ? 0.0 : a.getY(); // Happens on this line
System.out.println(x);
}
class A {
Double y = null;
private Double getY() {
return y;
}
}
public static void main(String[] args) {
new Example();
}
}
What's causing the NullPointerException
?
It happens because 0.0
is of type double
, not Double
. The second two operands to the conditional operator must be of the same type, so autoboxing/unboxing came into it and the compiler turned that code into:
x = Double.valueOf(a == null ? 0.0 : a.getY().doubleValue());
// -^^^^^^^^^^^^^^^--------------------------^^^^^^^^^^^^^^
...which throws because a.getY()
returns null
, and then the code tries to call doubleValue
on null
.
If we run javap -c Example
to decompile the code, we can see those calls (I've put them in bold):
public class Example { public Example(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."":()V 4: dconst_0 5: invokestatic #2 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double; 8: astore_1 9: new #3 // class Example$A 12: dup 13: aload_0 14: invokespecial #4 // Method Example$A."":(LExample;)V 17: astore_2 18: aload_2 19: ifnonnull 26 22: dconst_0 23: goto 33 26: aload_2 27: invokestatic #5 // Method Example$A.access$000:(LExample$A;)Ljava/lang/Double; 30: invokevirtual #6 // Method java/lang/Double.doubleValue:()D 33: invokestatic #2 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double; 36: astore_1 37: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream; 40: aload_1 41: invokevirtual #8 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V 44: return public static void main(java.lang.String[]); Code: 0: new #9 // class Example 3: dup 4: invokespecial #10 // Method "":()V 7: pop 8: return }
这篇关于为什么三元条件表达式返回 null 并分配给引用类型会导致 NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!