简体   繁体   中英

NullPointerException on Null Check

I am messed up with the below Code Cannot able to distinguish between the two code Snippets:

String Check=null;

if(Check!=null && Check.isEmpty()){
  System.out.println("Get Inside");
}

Above Code works Fine and Print the Message.

if(Check==null && Check.isEmpty()){
  System.out.println("get Inside")
}

this code will throw the NullPointerException.Not able to distinguish between this Code Please Help.

The && operator is a short-circuit , it doesn't evaluate the right hand side if it's not necessary. In this case, it's necessary:

if(Check==null && Check.isEmpty()){
  System.out.println("get Inside")
}

If Check is null (and that's the case), you'll continue to check the condition after the && , and you get Check.isEmpty() . Since Check is null , it's like null.isEmpty() which is a NEP.

Why?

When two expressions have && between them, if the first one is false, the answer will always be false and the other side won't be checked, but when the first is true , you have to continue and check the other side to know if all the expression should be evaluated to true or false .

But when you have:

if(Check!=null && Check.isEmpty()){
     System.out.println("get Inside")
}

Then the right side won't be reached, since it's redundant to check it. You already got false , and it doesn't matter if you get true or false on the right side, the result will be false since this is an AND ( FALSE && WHATEVER = FALSE ). So why checking?

This link might help you.

(I recommend you to follow Java Naming Conventions and make the variables begin with a lowercase).

&& means evaluating the left side first, if false then the whole expression is false . If the left side is true , it evaluates the right side. In your case:

if(Check==null && Check.isEmpty())

if Check is null , next it evaluates Check.isEmpty() which throws NullPointerException .

if(Check==null && Check.isEmpty())

if Check really equals null the in the other condition you're just saying null.isEmpty which ofcourse will lead to NullPointerException since && is irrelevant and pointless you needed || instead

if(Check==null || Check.isEmpty()){
  //no NullPointerException
  System.out.println("get Inside")
}

First let me tell you the difference between logical OR(||) and logical AND(&&) . When you use logical OR , if the first expression is TRUE , the second expression is not evaluated ; that means if any one expression is TRUE, the condition is satisfied and the flow passes into the loop. But in case of AND, all the expressions must satisfy to TRUE to satisfy the condition . And if any one of the expression evaluates to be FALSE, the condition is not satisfied.

Also keep in mind, we get a NPE(Null Pointer Exception) when we try to call methods upon variables whose value = null .

So in the First Case:

Check != null : the first condition is false, no further processing as logical AND returns FALSE if one of the expression is FALSE. Second expression is not evaluated (Check.isEmpty or whatsoever). No NPE as we are not calling methods upon any null value.

Second Case:

First Condition is satisfied, so second condition is evaluated which throws NPE as Check = null and we are call method upon a null value.

Hope you get it!

if(Check==null && Check.isEmpty()){
  System.out.println("get Inside")
}

In above Check is null so it goes and look up for next condition which is isEmpty that throws NullPointerException.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM