java 空检查抛出 NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27074430/
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
Null Check throws NullPointerException
提问by prsutar
Sometimes it happens that I have written a code to check NullPointerException like below,
有时我写了一个代码来检查 NullPointerException,如下所示,
if(str!=null)
{
doSomething();
}
and the null check itself throws NUllPointerException.
并且空检查本身会抛出 NUllPointerException。
How to solve it.
如何解决。
Edited: Actually this code was getting null pointer,
编辑:实际上这段代码正在获取空指针,
Map params = new HashMap();
if(params != null && params.get("requestType"))
{
//some null safe code goes here
}
i understood later that params.get() was throwing null pointer exception.
后来我了解到 params.get() 正在抛出空指针异常。
回答by Stephen C
You seem to be saying that:
你好像在说:
if (str != null) {
doSomething();
}
is throwing a NullPointerException
in the comparison.
NullPointerException
在比较中抛出一个。
That is impossible. So I expect that what is really happening is one of the following:
那是不可能的。所以我希望真正发生的事情是以下之一:
You have misinterpreted the stack trace, and the NPE is not being thrown there.
The actual code is substantially different to the illustrative example.
You are not executing the code that you think you are executing. For example, you might not have recompiled it ... or you may be executing a stale copy of the
.class
or.jar
file.You have managed to seriously confuse your IDE. (Sometimes you can get very strange behaviour from a confused IDE ...)
您误解了堆栈跟踪,并且 NPE 没有被扔到那里。
实际代码与说明性示例有很大不同。
您没有执行您认为正在执行的代码。例如,您可能没有重新编译它……或者您可能正在执行
.class
或.jar
文件的陈旧副本。您已经成功地严重混淆了您的 IDE。(有时你会从一个混乱的 IDE 中得到非常奇怪的行为......)
The only situations where that code might give an NPE that is not an artefact of something else you are doing wrong is if have a damaged Java (or IDE) installation, or if your hardware is faulty. I would discount those explanations as basically implausible.
该代码可能会导致 NPE 不是您做错的其他事情的人工制品的唯一情况是 Java(或 IDE)安装损坏,或者您的硬件出现故障。我认为这些解释基本上是不可信的。
Update
更新
Now you say that:
现在你说:
Map params = new HashMap();
if(params != null && params.get("requestType"))
{
//some null safe code goes here
}
throws an NPE in params.get
. I have to say that this is nonsense.
在params.get
. 我不得不说这是无稽之谈。
The code won't compile. The
params.get
call doesn't return something that isboolean
or that can be automatically converted to aboolean
.If we ignore the compilation errors, then
params.get
on a thread-confined map can only throw an NPE ifparams
isnull
. This map isthread-confined, and the previous check ensures thatparams
isn'tnull
.
代码不会编译。该
params.get
调用不会返回已经boolean
或可以自动转换为boolean
.如果我们忽略编译错误,那么
params.get
在线程受限映射上如果params
是则只能抛出 NPEnull
。这个映射是线程限制的,之前的检查确保它params
不是null
.
My previous conclusions stand. This is impossible.
我之前的结论成立。这是不可能的。
Hint: this couldbe a threading problem. It is possible to get intermittent NPE's if you update a HashMap
with one thread and read it with another, and you don't synchronize properly.
提示:这可能是线程问题。如果您HashMap
使用一个线程更新 a并使用另一个线程读取它,并且您没有正确同步,则可能会出现间歇性 NPE 。
回答by girish babu velivela
Have you checked it properly? Can you explain me scenario? Is it in java? If it is in Java then here is a sample code that works fine you cross check. If it is a local variable also it works fine.
你检查对了吗?你能给我解释一下场景吗?是在java中吗?如果它是在 Java 中,那么这里是一个示例代码,可以很好地进行交叉检查。如果它是一个局部变量,它也可以正常工作。
class string
{
static String str;
public static void main(String[] args){
if(str!=null){
System.out.println("hi");
}else{
System.out.println("bye");
}
}
}