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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:09:51  来源:igfitidea点击:

Null Check throws NullPointerException

javaexceptionnullpointerexceptionnull

提问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 NullPointerExceptionin 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 .classor .jarfile.

  • 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. 我不得不说这是无稽之谈。

  1. The code won't compile. The params.getcall doesn't return something that is booleanor that can be automatically converted to a boolean.

  2. If we ignore the compilation errors, then params.geton a thread-confined map can only throw an NPE if paramsis null. This map isthread-confined, and the previous check ensures that paramsisn't null.

  1. 代码不会编译。该params.get调用不会返回已经boolean或可以自动转换为boolean.

  2. 如果我们忽略编译错误,那么params.get在线程受限映射上如果params是则只能抛出 NPE null。这个映射线程限制的,之前的检查确保它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 HashMapwith 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");
           }
        }

    }