java Java短路评估
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1816776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java short circuit evaluation
提问by Donnie
I thought Java had short circuit evaluation, yet this line is still throwing a null pointer exception:
我以为 Java 有短路评估,但这一行仍然抛出空指针异常:
if( (perfectAgent != null) && (perfectAgent.getAddress().equals(entry.getKey())) ) {
In this case perfectAgentis null, so I just want the whole expression to return false, but my app is still crashing on this line with a NullPointerException.
在这种情况下perfectAgent是null,所以我只希望整个表达式返回false,但我的应用程序仍然在这条线上崩溃并出现 NullPointerException。
EDIT, general response:
编辑,一般反应:
Since perfectAgentis 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 perfectAgentdoes 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.
由于perfectAgentis null,&&不应执行右侧的任何内容,因为表达式不可能为真。更重要的是,它不可能执行,perfectAgent.getAddress()因为perfectAgent它不包含有效的引用(它是 null 和全部)。我正在尝试使用短路评估来不必在单独的语句中检查 null,因为这会使逻辑更加草率。
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.
编辑 2(或者,我是个白痴):是的,就像生活中的许多事情一样,你在向全世界宣布你是个白痴后马上就找到了答案。在这种情况下,我在做其他事情时关闭了 Eclipse 的自动构建,并且没有重新打开它,所以我正在调试与我的源不匹配的类文件。
采纳答案by Stephen C
Advanced debugging lesson #1:
高级调试课程#1:
If you run into a seemingly impossible error (e.g. one that contradicts you knowledge about Java), do the following:
如果您遇到看似不可能的错误(例如,与您对 Java 的知识相矛盾的错误),请执行以下操作:
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.
总之,要学会多怀疑自己。
回答by Jon Skeet
If perfectAgentis genuinely null, that code won'tthrow 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.
如果perfectAgent真的为空,则该代码不会抛出异常(至少假设没有发生奇怪的线程处理,在表达式中途将其从非空更改为空)。如果你能制作一个简短但完整的程序来演示它,我会感到非常震惊。
So yes, your intuition is right - this shouldn't be a problem. Look elsewhere for the cause. I strongly suspect that perfectAgentisn'tactually null, and that you're running into any of the other situations in that code which could cause an exception.
所以是的,你的直觉是对的 - 这应该不是问题。在别处寻找原因。我强烈怀疑这实际上perfectAgent不是空的,并且您在该代码中遇到了可能导致异常的任何其他情况。
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 perfectAgentreally isnull? Try inserting this code before it:
是什么让你认为那perfectAgent真的是空的?尝试在它之前插入此代码:
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.
另一个非常非常小的可能性是您遇到了 JIT 错误 - 但我非常怀疑。
回答by Asaph
Java doeshave short circuit evaluation. Perhaps entryis nulland so entry.getKey()is causing the NullPointerException. Another possibility is that getAddress()either returns nullor has a NullPointerExceptionhappening inside somewhere (if it's more complicated than a simple returnstatement).
Java确实有短路评估。也许entry是null等entry.getKey()正在导致NullPointerException. 另一种可能性是getAddress()返回null或NullPointerException在某处发生(如果它比简单的return语句更复杂)。
EDIT: I see your edit where you claim this:
编辑:我看到你的编辑,你声称这一点:
More to the point, it is impossible to execute
perfectAgent.getAddress()...
更重要的是,不可能执行
perfectAgent.getAddress()...
But what if perfectAgent.getAddress()issuccessfully executed and returnsnull? See what I mean...
但是,如果perfectAgent.getAddress()被成功执行和回报null?明白了吗...
回答by Jim Ferrans
You ensure that perfectAgentis not null, so one or more of perfectAgent.getAddress()or entryor entry.getKey()must be null. Or getAddress() or getKey() are hitting an NPE in their implementation.
你保证perfectAgent不为空,那么一个或多个perfectAgent.getAddress()或entry或entry.getKey()必须为空。或者 getAddress() 或 getKey() 在它们的实现中遇到了 NPE。
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.)
要调试此类事情,请先查看堆栈跟踪以确定位置。这将告诉您它是否发生在 getAddress() 或 getKey() 或调用它们的粘贴代码片段中。接下来,如果它在这个片段中,在 if 之前添加一些代码来测试哪个是空的。您可以使用旧的 System.err.println() 或assertions。(如果您使用断言,请确保使用 java 命令的 -enableassertions 标志启用它们。)
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.
更新:所以我的解释结果是错误的......问题提出了两个相互矛盾的事实(这条线上有一个 NPE,但短路应该发生了),我自动假设第一个事实是真的,第二个是假的实际上,由于关闭了 Eclipse 中的自动构建,这完全是一个不同的问题。呸!在调试“不可能”的事情时,彻底怀疑是有帮助的。
回答by Jim Garrison
There are three references other than perfectAgent that could be null:
除了 PerfectAgent 之外,还有三个引用可能为 null:
- perfectAgent.getAddress()
- entry
- entry.getKey()
- PerfectAgent.getAddress()
- 入口
- entry.getKey()
Break up the statement or run it in a debugger.
分解语句或在调试器中运行它。
回答by Andreas Dolk
Big mistery. I copied your line of code and tested with perfectAgent == null, entry == null, entry.getKey() == nulland combinations of those: No NPE in my test bed (Java 1.6).
大谜团。我复制你的代码行,并与测试perfectAgent == null,entry == null,entry.getKey() == null和这些的组合:(Java 1.6的)无NPE在我的测试床。
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 :)
不管它是什么烦人的错误,我怀疑它与短路评估有关。如果是这条线导致 NPE,那么据我所知,perfectAgent 不为空。祝你好运 - 一旦你发现了错误,请向我们展示它:)
回答by Nathan Feger
Try formatting your code like this:
尝试像这样格式化你的代码:
if(
(perfectAgent != null)
&& (
perfectAgent.getAddress()
.equals(
entry.getKey()
)
)
) {
It should give you a better stack trace line entry.
它应该为您提供更好的堆栈跟踪行条目。

