-1

So I've done a some readings on NullPointerExceptions in Java but I'm still not really understanding it completely.

why does this work?

if ((department != null && department.equals("COMP")) || (department != null && department.equals("COMM")))
{
    this.department = department;
}

and also another method that worked was when I first checked for != null and then did a second nested if statement to then check for "COMP" or "COMM".

compared to the above, how come this one doesn't work?

if (department != null || department.equals("COMP")) || department.equals("COMM")))
{
    this.department = department;
}

Like most, I don't like having found a solution by accident but not really understanding why it's a solution. I'm still very new to programming so I'm trying to understand what's actually happening underneath the hood. I understand things the easiest when given a metaphor to compare with, it'd really help if someone can explain it for me that way ><;;

Thank you guys very much!

sKyLineLOL
  • 131
  • 1
  • 8
  • What's department? And second, the 2nd code snippet ain't the same logic – Andrew Li Jun 08 '16 at 04:34
  • Well if you try to call any method on an object not initialized it throws an exception to prevent that we use checks like department != null , regarding the question studying a bit of logical operator in java may help. – Vinay Jun 08 '16 at 04:44
  • Possible duplicate of [Java logical operator short-circuiting](http://stackoverflow.com/questions/8759868/java-logical-operator-short-circuiting) – Nier Jun 08 '16 at 06:07
  • @Nier I don't think that is the issue exactly for OP. OP is misunderstanding the boolean algebra between the two if statements (although I'm sure there is definitely a duplicate for that). – But I'm Not A Wrapper Class Jun 08 '16 at 12:20

2 Answers2

1

The answer to your question is in the boolean algebra and how you are implementing the conditions

Doing this

((department != null && department.equals("COMP")) || (department != null && department.equals("COMM")))

is equivalent to doing

A&B | A&C that can be resumed to A&(B|C).... so A must be met AND either B OR C in order to execute the code...

so far so good.

the second condition

if (department != null || department.equals("COMP")) || department.equals("COMM")))

is equivalent to doing

A | B | C that can NOT be resumed/simplified to anything

.... so A must be met OR either B OR C in order to execute the code...

if A is null first condition fails, then java tries to check condition B but since B is null it explodes with a nice NullPointer Exception

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Because of boolean logic and because of runtime optimization.

In a boolean sentence if all operands are AND and one sentence is False all the sentence is False. So when runtime evaluate the first sentence and discover that is false it doesn't evaluate the others AND sentence. Than for AND case, no NullPointerExceptions exception, that means try to access an object that is null, may happen if you check it first. Otherwise if the sentence contains Only OR operand, runtime have to evaluate all the sentence, so the Null pointer exception happens because the object is null, the first sentence in OR is safe because you can compare a null object to NULL, but the second is not, because you cannot access a null object property.

christian mini
  • 1,662
  • 20
  • 39