Java 将 String 与 null 进行比较时,为什么会出现 NullPointerException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2358019/
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
Why do I get a NullPointerException when comparing a String with null?
提问by rockit
My code is breaking on the following line with a nullpointerexception:
我的代码在以下行中出现空指针异常:
if (stringVariable.equals(null)){
Previous to this statement, I declare the stringVariable and set it to a database field.
在此语句之前,我声明了 stringVariable 并将其设置为数据库字段。
In this statement, I am trying to detect if the field had a null
value, but unfortunately it breaks!
在这个语句中,我试图检测该字段是否有null
值,但不幸的是它坏了!
Any thoughts?
有什么想法吗?
采纳答案by Pool
Use
用
stringVariable == null
To test whether stringVariable
is null
.
来测试是否stringVariable
是null
。
The equals
method (and every other method) requires stringVariable
to not be null
.
该equals
方法(以及所有其他方法)要求stringVariable
不是null
.
回答by extraneon
It is never wise to call a method, be it equals() or otherwise,on a variable which may be null. That is why one usually does something like:
在可能为 null 的变量上调用方法,无论是 equals() 还是其他方式,都是不明智的。这就是为什么人们通常会做这样的事情:
if ( var != null && var.method(something) ) {
// var.method() was true
} else {
// var is null or var.method is false
}
In your special case it would be sufficient to do
在您的特殊情况下,这样做就足够了
if (stringVariable == null) {
}
when working with Strings it can pay to check out Apache Commons StringUtils.
使用字符串时,可以付费查看Apache Commons StringUtils。
It always pays to check out the apache commons libraries as they have lots of optimized utilities (for Strings, Collections, Dates and such) which tend to be better than home-written ones.
检查 apache 公共库总是值得的,因为它们有许多优化的实用程序(用于字符串、集合、日期等),往往比自制的更好。
回答by Jama22
if stringvariable
is already null, it doesn't exist as a String
object anymore, so it won't even have a .equals
method! So in the event when stringvariable
isnull, what you are really doing is null.equals(null)
, at which point you'll get the NullPointerException
because null
doesn't have a .equals()
method.
如果stringvariable
已经为空,它不再作为String
对象存在,所以它甚至没有.equals
方法!因此,如果 whenstringvariable
为null,您真正在做的是null.equals(null)
,此时您将得到NullPointerException
因为null
没有.equals()
方法。
回答by s_ag
The equals() method does not work with null parameter.
equals() 方法不适用于 null 参数。
The method needs to have an object or string as parameter.
该方法需要有一个对象或字符串作为参数。
public boolean equals(Object anObject)
Since null is not an object.Is null an Object?.
因为 null 不是一个对象。null 是一个对象吗?.
Also refer to java 7 documentation which says that equals will give result if and only if the object passed is not null.
另请参阅 java 7 文档,其中说当且仅当传递的对象不为 null 时,equals 才会给出结果。
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equals(java.lang.Object)
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equals(java.lang.Object)