生产环境上出现空指针异常,追踪报错位置得知以下代码报错
if (isNull(aiGroup)) {
return null;
}
aiGroup.setNum(isNull(param.getNum()) ? aiGroup.getNum() : param.getNum().doubleValue());
乍一看,真没有什么问题(当然可能是我经验不足),细看会发现自动装箱导致空指针异常,上边set方法代码可以拆分为两行:
Double num = isNull(param.getNum()) ? aiGroup.getNum() : param.getNum().doubleValue();
aiGroup.setNum(num);
声明:属性类型:aiGroup#num类型是Double,param#num类型是BigDecimal
你可以自己单独写一个main方法,自行运行一下。
在这里我直接说出来,以上代码在获取num时,如果isNull()方法为true时,会从aiGroup获取num,但是aiGroup.getNum()的结果是null,理论是你直接给一个包装类型属性设置null是没有问题的,比如aiGroup.setNum(null),这样是正确的。
但是三元运算的时候,如果发现结果类型和表达式中的类型不一致,他会在最外层进行自动装箱,会执行Double.valueOf()的操作,所以会出现空指针的现象:Double.valueOf(null)
。
反之会出现自动拆箱问题。
许多人可能会不明白我是怎么知道为什么会有Double.valueOf(null)
这一步,过程很简单,查看Java的字节码就可以看到,想看Java字节码更详细的点可以看:Java字节码介绍。
言归正传,在这里为了简单我又新建了一个简单Main类,使三元运算中表达式的类型不一致,代码如下:
public class Main {
public static void main(String[] args) {
SdAiGroup aiGroup = new SdAiGroup();
// aiGroup#num是Double类型,aiGroup#test是double类型
Double test = Objects.isNull(aiGroup.getId()) ? aiGroup.getNum() : aiGroup.getTest();
aiGroup.setAcos(test);
System.err.println(test);
}
}
生成class文件,然后执行Javap -c Main.class
结果如下:
Compiled from "Main.java"
public class com.sparkxmedia.xmars.ai.center.service.impl.Main {
public com.sparkxmedia.xmars.ai.center.service.impl.Main();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."":()V
4: return
public static void main(java.lang.String[]);
Code:
0: new #2 // class com/sparkxmedia/xmars/ai/center/model/entity/SdAiGroup
3: dup
4: invokespecial #3 // Method com/sparkxmedia/xmars/ai/center/model/entity/SdAiGroup."":()V
7: astore_1
8: aload_1
9: invokevirtual #4 // Method com/sparkxmedia/xmars/ai/center/model/entity/SdAiGroup.getId:()Ljava/lang/Long;
12: invokestatic #5 // Method java/util/Objects.isNull:(Ljava/lang/Object;)Z
15: ifeq 25
18: aload_1
19: invokevirtual #6 // Method com/sparkxmedia/xmars/ai/center/model/entity/SdAiGroup.getTest:()D
22: goto 29
25: aload_1
26: invokevirtual #6 // Method com/sparkxmedia/xmars/ai/center/model/entity/SdAiGroup.getTest:()D
29: invokestatic #7 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
32: astore_2
33: aload_1
34: aload_2
35: invokevirtual #8 // Method com/sparkxmedia/xmars/ai/center/model/entity/SdAiGroup.setAcos:(Ljava/lang/Double;)V
38: getstatic #9 // Field java/lang/System.err:Ljava/io/PrintStream;
41: aload_2
42: invokevirtual #10 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
45: return
}
可以看到第29行:Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
将结果执行方法Double.valueOf(null);
反之我们将三元运算中表达式的类型和结果类型一致,代码如下:
将Main方法中三元运算符替换为:Double test = Objects.isNull(aiGroup.getId()) ? aiGroup.getNum() : aiGroup.getNum();
结果如下:
Compiled from "Main.java"
public class com.sparkxmedia.xmars.ai.center.service.impl.Main {
public com.sparkxmedia.xmars.ai.center.service.impl.Main();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."":()V
4: return
public static void main(java.lang.String[]);
Code:
0: new #2 // class com/sparkxmedia/xmars/ai/center/model/entity/SdAiGroup
3: dup
4: invokespecial #3 // Method com/sparkxmedia/xmars/ai/center/model/entity/SdAiGroup."":()V
7: astore_1
8: aload_1
9: invokevirtual #4 // Method com/sparkxmedia/xmars/ai/center/model/entity/SdAiGroup.getId:()Ljava/lang/Long;
12: invokestatic #5 // Method java/util/Objects.isNull:(Ljava/lang/Object;)Z
15: ifeq 25
18: aload_1
19: invokevirtual #6 // Method com/sparkxmedia/xmars/ai/center/model/entity/SdAiGroup.getTest:()D
22: goto 29
25: aload_1
26: invokevirtual #6 // Method com/sparkxmedia/xmars/ai/center/model/entity/SdAiGroup.getTest:()D
29: invokestatic #7 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
32: astore_2
33: aload_1
34: aload_2
35: invokevirtual #8 // Method com/sparkxmedia/xmars/ai/center/model/entity/SdAiGroup.setAcos:(Ljava/lang/Double;)V
38: getstatic #9 // Field java/lang/System.err:Ljava/io/PrintStream;
41: aload_2
42: invokevirtual #10 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
45: return
}
发现没有Double.valueOf(null)方法了;
根据以上的自动装箱问题,你可以自己试着写个Main方法,试试自动拆箱问题,
比如
最根本的问题就是自动拆装箱导致的问题,而三元运算只是问题的引发,更多的自动拆箱和装箱问题,如果不清楚的话, 可以自行google或者留言。
--------------------------------------------------------------欢迎叨扰此地址---------------------------------------------------------------
本文作者:Java技术债务
原文链接:https://cuizb.top/myblog/article/detail/1690515044
版权声明: 本博客所有文章除特别声明外,均采用 CC BY 3.0 CN协议进行许可。转载请署名作者且注明文章出处。