6

Sometimes it happens that I have written a code to check NullPointerException like below,

if(str!=null)
{
    doSomething();
} 

and the null check itself throws 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.

Puneet Verma
  • 1,373
  • 2
  • 19
  • 23
prsutar
  • 429
  • 2
  • 4
  • 17
  • Can you post your full code ?? – Anik Islam Abhi Nov 22 '14 at 05:34
  • 1
    You don't have code to "check NullPointerException" as you say, but you have code that checks for `null`. It is impossible for this code itself to throw a NullPointerException, so the problem must be somewhere else in your code, in a place that you haven't posted yet to SO. – Erwin Bolwidt Nov 22 '14 at 05:38
  • 1
    that check didn't throw exceptions. perhaps you did if (str!=null); { } or if (str.foobar() != null) { } – MeBigFatGuy Nov 22 '14 at 05:48
  • How could one throws NullPointerException when it wont deal with object manipulation? – janasainik Nov 24 '14 at 08:31

2 Answers2

14

You seem to be saying that:

if (str != null) {
    doSomething();
}

is throwing a NullPointerException in the comparison.

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 ...)


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.


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.

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

  2. If we ignore the compilation errors, then params.get on a thread-confined map can only throw an NPE if params is null. This map is thread-confined, and the previous check ensures that params isn't null.

My previous conclusions stand. This is impossible.

Hint: this could be 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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • recently i have been working on a support project, that file was jsp file and code written in scriptlet was giving NPE at the condition like above, so asked the question, thnx for reply. – prsutar Nov 24 '14 at 09:14
  • My conclusion is that the code in the actual project was substantially different to the examples in your question. I'm afraid we can't give you meaningful answers if you show us the wrong code. – Stephen C May 06 '19 at 12:42
0

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.

    class string 
    {
        static String str;

        public static void main(String[] args){
           if(str!=null){
               System.out.println("hi");
           }else{
               System.out.println("bye");
           }
        }

    } 
Krishna
  • 484
  • 7
  • 22
  • 1
    This is not relevant to the question. The question is about whether `str != null` throws an exception. – Stephen C May 06 '19 at 13:31