I thought Java had short circuit evaluation, yet this line is still throwing a null pointer exception:
if( (perfectAgent != null) && (perfectAgent.getAddress().equals(entry.getKey())) ) {
In this case perfectAgent
is null
, so I just want the whole expression to return false
, but my app is still crashing on this line with a NullPointerException.
EDIT, general response:
Since perfectAgent
is null
, nothing to the right of the &&
should be executed, as it is impossible for the expression to be true. More to the point, it is impossible to execute perfectAgent.getAddress()
since perfectAgent
does not contain a valid reference (it being null and all). I'm trying to use short circuit evaluation to not have to check for null in a seperate statement as that makes the logic more sloppy.
EDIT 2 (or, I'm an idiot): Yeah, like many things in life you figure out the answer right after announcing to the world that you're a moron. In this case, I had turned off Eclipse's autobuild while doing something else and not turned it back on, so I was debugging class files that didn't match up with my source.
So when Java finds the value on the left side of an && operator to be false, then Java gives up and declares the entire expression to be false. That's called short circuit expression evaluation.
Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first ...
In Java logical operators, if the evaluation of a logical expression exits in between before complete evaluation, then it is known as Short-circuit. A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned.
The difference is that in case of lazy evaluation an expression is evaluated only when it is needed, while in case of short-circuit evaluation expression evaluation stops right after you know the result.
If perfectAgent
is genuinely null, that code won't throw an exception (at least assuming there aren't weird threading things going on, changing it from non-null to null half way through the expression). I would be utterly shocked if you could produce a short but complete program demonstrating it doing so.
So yes, your intuition is right - this shouldn't be a problem. Look elsewhere for the cause. I strongly suspect that perfectAgent
isn't actually null, and that you're running into any of the other situations in that code which could cause an exception.
I suggest you try to extract that bit of code out into a short but complete example - if you can do so, I'll eat my metaphorical hat; if not, you'll hopefully find the problem while you attempt the extraction.
What makes you think that perfectAgent
really is null? Try inserting this code before it:
if (perfectAgent == null)
{
System.out.println("Yup, it's null");
}
Another very, very slim possibility is that you've run into a JIT bug - but I highly doubt it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With