带有布尔值的 java.lang.NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25824269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NullPointerException with boolean
提问by Alejandro Garcia
I wrote a realy simple code based on another question and here it is:
我根据另一个问题编写了一个非常简单的代码,这里是:
It throws me an error
它给我一个错误
java.lang.NullPointerException line 5 and 17
java.lang.NullPointerException 第 5 行和第 17 行
I don't know what I'm doing wrong.
我不知道我做错了什么。
public class Main {
public static String bool(Boolean param){
if(param == true){ (line 5)
return "a";
}else if(param == false){
return "b";
}
return "c";
}
public static void main(String[] args){
System.out.println(bool(true));
System.out.println(bool(null)); (line 17)
System.out.println(bool(false));
}
}
回答by Marko Topolnik
nullcannot be auto-unboxed to a primitive booleanvalue, which is what happens when you try to compare it with true. In
null无法自动拆箱为原始boolean值,当您尝试将其与true. 在
param == true
The type of trueis boolean, therefore the left-hand operand must also be a boolean. You are passing in a Boolean, which is an object, but can be auto-unboxed to boolean.
trueis的类型boolean,因此左边的操作数也必须是 a boolean。您传入的是Boolean,这是一个对象,但可以自动拆箱为boolean。
Therefore this is equivalent to
因此这相当于
param.booleanValue() == true
Clearly, if paramis null, the above throws NullPointerException.
显然,如果param是null,则上面会抛出NullPointerException。
To avoid the hidden pitfalls of auto-unboxing, you could instead work with the Booleanobjects:
为了避免自动拆箱的隐藏陷阱,您可以改为使用Boolean对象:
if (Boolean.TRUE.equals(param))
return "a";
if (Boolean.FALSE.equals(param))
return "b";
return "c";
回答by Mureinik
Your code compares a java.lang.Booleaninstance with a primitive boolean, which means unboxing the java.lang.Boolean. Since nullcan't be unboxed, a NullPointerExceptionis thrown.
您的代码将java.lang.Boolean实例与原语进行比较boolean,这意味着将java.lang.Boolean. 由于null无法拆箱,NullPointerException因此抛出 a 。
You could work around this by using the built in constants Boolean.TRUEand Boolean.FALSE:
您可以通过使用内置常量Boolean.TRUE和来解决这个问题Boolean.FALSE:
public static String bool(Boolean param) {
if (Boolean.TRUE.equals(param)) {
return "a";
} else if (Boolean.FALSE.equals(param)) {
return "b";
}
return "c";
}
回答by Govind
So your program must be something like this.
所以你的程序一定是这样的。
public class BooleanBug {
public static String bool(Boolean param) {
if ((null != param) && param.booleanValue() == true) {
return "a";
} else if ((null != param) && param.booleanValue() == false) {
return "b";
}
return "c";
}
public static void main(String[] args) {
System.out.println(bool(true));
System.out.println(bool(null));
System.out.println(bool(false));
}
}
回答by Alex
You used the Booleaninstead of boolean. Booleanis a class, which means you can assign objects to it. In your case, you passed in a null, which is then assigned to param. You then tried to use param, which of course resulted in a NullPointerException.
您使用了Boolean而不是boolean。Boolean是一个类,这意味着您可以为其分配对象。在您的情况下,您传入了 a null,然后将其分配给 param。然后您尝试使用 param,这当然会导致NullPointerException.
You can:
你可以:
- Get rid of the line
bool(null) - Change
Booleantobooleanin the parameters forbool() - Add an else if for when
paramisnull
- 摆脱线
bool(null) - 更改
Boolean到boolean在参数bool() - 添加一个 else if for when
paramisnull

