为什么三元条件表达式返回 null 并分配给引用类型会导致 NullPointerException? [英] Why does a ternary conditional expression returning null and assigned to a reference type cause a NullPointerException?

查看:23
本文介绍了为什么三元条件表达式返回 null 并分配给引用类型会导致 NullPointerException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢在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.0double 类型,而不是 Double 类型.条件运算符的后两个操作数必须是相同类型,因此自动装箱/拆箱进入其中,编译器将该代码转换为:

x = Double.valueOf(a == null ? 0.0 : a.getY().doubleValue());//-^^^^^^^^^^^^^^^----------^^^^^^^^^^^^^^^

...抛出,因为 a.getY() 返回 null,然后代码尝试在 上调用 doubleValuenull.

如果我们运行 javap -c Example 来反编译代码,我们可以看到那些调用(我把它们加粗了):

<前>公共类示例{公共示例();代码:0:aload_01: invokespecial #1//方法 java/lang/Object."":()V4:dconst_05: invokestatic #2//方法 java/lang/Double.valueOf:(D)Ljava/lang/Double;8:astore_19: new #3//类 Example$A12:重复13:aload_014: invokespecial #4//方法示例$A."":(LExample;)V17:astore_218:aload_219:如果非空 2622:dconst_023:转到 3326:aload_227: invokestatic #5//方法示例$A.access$000:(LExample$A;)Ljava/lang/Double;30: invokevirtual #6//方法 java/lang/Double.doubleValue:()D33: invokestatic #2//方法 java/lang/Double.valueOf:(D)Ljava/lang/Double;36:astore_137: getstatic #7//字段 java/lang/System.out:Ljava/io/PrintStream;40:aload_141: invokevirtual #8//方法 java/io/PrintStream.println:(Ljava/lang/Object;)V44:返回public static void main(java.lang.String[]);代码:0: new #9//类示例3:重复4: invokespecial #10//方法 "":()V7:流行8:返回}

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆