Java short circuit evaluation

回眸只為那壹抹淺笑 提交于 2019-11-30 22:00:27

问题


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.


回答1:


Advanced debugging lesson #1:

If you run into a seemingly impossible error (e.g. one that contradicts you knowledge about Java), do the following:

  • Consult a reputable text book (or better still, the relevant standard) to confirm that your understanding is not flawed. (In this case your understanding was correct, and any half-decent textbook would confirm this in a minute.)

  • Check all of the stupid things that you could have done that could cause the impossible error. Things like not saving a file, not doing a complete build, running an old / stale version of the application, being in the wrong directory, and so on.

In summary, learn to doubt yourself a bit more.




回答2:


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.




回答3:


Java does have short circuit evaluation. Perhaps entry is null and so entry.getKey() is causing the NullPointerException. Another possibility is that getAddress() either returns null or has a NullPointerException happening inside somewhere (if it's more complicated than a simple return statement).

EDIT: I see your edit where you claim this:

More to the point, it is impossible to execute perfectAgent.getAddress() ...

But what if perfectAgent.getAddress() is successfully executed and returns null? See what I mean...




回答4:


You ensure that perfectAgent is not null, so one or more of perfectAgent.getAddress() or entry or entry.getKey() must be null. Or getAddress() or getKey() are hitting an NPE in their implementation.

To debug this sort of thing, look first at the stack trace to pin down the location. This would tell you if it's happening in getAddress() or in getKey() or in the pasted code snippet that calls them. Next, if it's in this snippet, add some code before the if to test which is null. You can use good old System.err.println() or assertions. (If you use assertions, be sure to enable them with the java command's -enableassertions flag.)

Update: So my interpretation turned out to be wrong ... the problem presented two contradictory facts (there was an NPE on this line and yet the short-circuit should have happened) and I automatically assumed the first fact was true and the second false when in fact it was a different problem entirely due to turning off the auto-build in Eclipse. Duh! In debugging something "impossible" it helps to be radically skeptical.




回答5:


There are three references other than perfectAgent that could be null:

  • perfectAgent.getAddress()
  • entry
  • entry.getKey()

Break up the statement or run it in a debugger.




回答6:


Big mistery. I copied your line of code and tested with perfectAgent == null, entry == null, entry.getKey() == null and combinations of those: No NPE in my test bed (Java 1.6).

Whatever annoying bug it is, I doubt that it has something to do with short circuit evaluation. If it's this line causing NPE, than, as far as I can say, perfectAgent is not null. Good luck and - show us the bug once you've catched it :)




回答7:


Try formatting your code like this:

if( 
  (perfectAgent != null) 
  && (
      perfectAgent.getAddress()
      .equals(
       entry.getKey()
      )
     ) 
  ) {

It should give you a better stack trace line entry.



来源:https://stackoverflow.com/questions/1816776/java-short-circuit-evaluation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!