Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:一元if-npe_Java_If Statement_Nullpointerexception - Fatal编程技术网

Java:一元if-npe

Java:一元if-npe,java,if-statement,nullpointerexception,Java,If Statement,Nullpointerexception,为什么此代码会导致NPE?Findbugs给了我一个提示,这可能会发生,有时也会发生:-) 有什么想法吗 public Integer whyAnNPE() { return 1 == 2 ? 1 : 1 == 2 ? 1 : null; } 编辑:当我写这个答案时,问题中的代码不存在 这里有另一种方法让它稍微清晰一些: public static Integer maybeCrash(boolean crash) { return true ? (crash ? null :

为什么此代码会导致NPE?Findbugs给了我一个提示,这可能会发生,有时也会发生:-)

有什么想法吗

public Integer whyAnNPE() {
    return 1 == 2 ? 1 : 1 == 2 ? 1 : null;
}

编辑:当我写这个答案时,问题中的代码不存在

这里有另一种方法让它稍微清晰一些:

public static Integer maybeCrash(boolean crash) {
    return true ? (crash ? null : 1) : 0;
}
重要的是这里有两个条件表达式。内部的类型为
Integer
,这是由于中规定的确定类型的最后一个项目符号

在这一点上,我们遇到了这样的情况:

public static Integer maybeCrash(boolean crash) {
    Integer tmp = null;
    return true ? tmp : 0;
}
condition ? null-type : int
condition ? Integer : int
现在,对于其余的条件表达式,前面的项目符号将应用并执行。这又作为第一步调用,但失败了

换言之,类似这样的条件:

public static Integer maybeCrash(boolean crash) {
    Integer tmp = null;
    return true ? tmp : 0;
}
condition ? null-type : int
condition ? Integer : int
可能涉及将
int
装箱为
Integer
,但条件如下:

public static Integer maybeCrash(boolean crash) {
    Integer tmp = null;
    return true ? tmp : 0;
}
condition ? null-type : int
condition ? Integer : int
可能涉及将
整数
解装箱到
int


原始答案

下面是一个相当简单的示例,它实际上是有效的Java:

public class Test {
    public static void main(String[] args) {
        int x = args.length == 0 ? 1 : null;
    }
}
这实际上是:

int tmp;
if (args.length == 0) {
   tmp = 1;
} else {
   Integer boxed = null;
   tmp = boxed.intValue();
}

显然,这里的拆箱步骤会非常成功。基本上,这是因为通过取消装箱将空表达式隐式转换为
Integer
,并从
Integer
转换为
int

编辑:在我编写此答案时,问题中的代码不存在

这里有另一种方法让它稍微清晰一些:

public static Integer maybeCrash(boolean crash) {
    return true ? (crash ? null : 1) : 0;
}
重要的是这里有两个条件表达式。内部的类型为
Integer
,这是由于中规定的确定类型的最后一个项目符号

在这一点上,我们遇到了这样的情况:

public static Integer maybeCrash(boolean crash) {
    Integer tmp = null;
    return true ? tmp : 0;
}
condition ? null-type : int
condition ? Integer : int
现在,对于其余的条件表达式,前面的项目符号将应用并执行。这又作为第一步调用,但失败了

换言之,类似这样的条件:

public static Integer maybeCrash(boolean crash) {
    Integer tmp = null;
    return true ? tmp : 0;
}
condition ? null-type : int
condition ? Integer : int
可能涉及将
int
装箱为
Integer
,但条件如下:

public static Integer maybeCrash(boolean crash) {
    Integer tmp = null;
    return true ? tmp : 0;
}
condition ? null-type : int
condition ? Integer : int
可能涉及将
整数
解装箱到
int


原始答案

下面是一个相当简单的示例,它实际上是有效的Java:

public class Test {
    public static void main(String[] args) {
        int x = args.length == 0 ? 1 : null;
    }
}
这实际上是:

int tmp;
if (args.length == 0) {
   tmp = 1;
} else {
   Integer boxed = null;
   tmp = boxed.intValue();
}

显然,这里的拆箱步骤会非常成功。基本上,这是因为空表达式隐式转换为
Integer
,并通过取消装箱从
Integer
转换为
int

这不是Java-它是什么语言?它仍然不是Java,但现在很明显,为什么它“有时”会给你NPE“这不是Java-它是什么语言?它仍然不是Java,但现在很明显为什么它“有时”会给你NPE,我不明白这在这里是如何适用的。他返回的是一个
整数
对象,所以空值无论如何都不会被解除绑定?他必须将其分配给一个
int
,在这种情况下,函数不是有bug的部分(我的意思是从返回整数的方法返回null非常好)@Voo:No,因为条件表达式的类型仍然是int-然后它又被装箱了。当我回答时,问题中的代码不存在-我现在就编辑。@Voo:看看它现在是否有意义:)是的,甚至你的评论都说得很清楚-但我的理解仍然不清楚,因为我认为
错误?1:null
将显示相同的行为。引用的语言规范和示例也澄清了这一点-谢谢,我很感激:D总是很高兴学习一门语言的一些复杂的角落案例。@Voo:我自己也不确定,直到我查了所有这些:)我不明白这在这里是如何适用的。他返回的是一个
整数
对象,所以空值无论如何都不会被解除绑定?他必须将其分配给一个
int
,在这种情况下,函数不是有bug的部分(我的意思是从返回整数的方法返回null非常好)@Voo:No,因为条件表达式的类型仍然是int-然后它又被装箱了。当我回答时,问题中的代码不存在-我现在就编辑。@Voo:看看它现在是否有意义:)是的,甚至你的评论都说得很清楚-但我的理解仍然不清楚,因为我认为
错误?1:null
将显示相同的行为。引用的语言规范和示例也澄清了这一点-谢谢,我很感激:D总是很高兴学习一些复杂的语言案例。@Voo:直到我查遍了,我才确定自己:)