Java:拆箱整数时出现空指针异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1811706/
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: null pointer exception when unboxing Integer?
提问by Nick Heiner
This code is causing a null pointer exception. I have no idea why:
此代码导致空指针异常。我不知道为什么:
private void setSiblings(PhylogenyTree node, Color color) throws InvalidCellNumberException {
PhylogenyTree parent = node.getParent();
for (PhylogenyTree sibling : parent.getChildren()) {
if (! sibling.equals(node)) {
Animal animal = sibling.getAnimal();
BiMap<PhylogenyTree, Integer> inverse = cellInfo.inverse();
int cell = inverse.get(animal); // null pointer exception here
setCellColor(cell, color);
}
}
}
I've examined it in the debugger, and all the local variables are non-null. How else could this be happening? The BiMap is from Google Collections.
我在调试器中检查过它,所有的局部变量都是非空的。这怎么可能发生?BiMap 来自 Google Collections。
采纳答案by notnoop
The null pointer exception is a result of unboxing the result of inverse.get(animal)
. If inverse
doesn't contain the key animal
, it returns null
, "of type" Integer
. Given that the assignment is to an int
reference, Java unboxes the value into an int
, resulting in a null pointer exception.
空指针异常是对 的结果拆箱的结果inverse.get(animal)
。如果inverse
不包含 key animal
,则返回null
, "of type" Integer
。鉴于赋值是对int
引用的,Java 将值拆箱为int
,从而导致空指针异常。
You should either check for inverse.containsKey(animal)
or use Integer
as the local variable type to avoid unboxing and act accordingly. The proper mechanism depends on your context.
您应该检查inverse.containsKey(animal)
或Integer
用作局部变量类型以避免拆箱并采取相应措施。正确的机制取决于您的上下文。
回答by OscarRyz
You must have a stacktrace. It says exactly what was the line where that happened. Post it and we can tell.
你必须有一个堆栈跟踪。它确切地说明了发生这种情况的线路。发布它,我们可以知道。
From all the posted code, I can "guess" one of these are a potential NullPointerException (NPE).
从所有发布的代码中,我可以“猜测”其中之一是潜在的 NullPointerException (NPE)。
node
may be null and calling node.getParent
.
node
可能为 null 并调用node.getParent
.
The node's parent may be null and invoking parent.getChildren
may throw an NPE.
节点的父节点可能为空,调用parent.getChildren
可能会抛出 NPE。
One of the siblings may be null and invoking sibling.equals
may throw an NPE.
兄弟姐妹之一可能为空,调用sibling.equals
可能会引发 NPE。
cellInfo may be null and cellInfo.inverse
will throw it.
cellInfo 可能为 null 并且cellInfo.inverse
会抛出它。
Finally the "inverse" returned may be null and inverse.get()
will throw it.
最后返回的“逆”可能为空inverse.get()
并将抛出它。
Phew!!...
呼!!...
So, to avoid doing this wild guessings, why don't you just post your stacktrace and we find out?
因此,为了避免进行这种疯狂的猜测,您为什么不发布您的堆栈跟踪,然后我们找出答案?
It should something like:
它应该类似于:
java.lang.NullPointerException: null
at YourClass.setSiblings( YouClass.java:22 )
at YourClass.setSiblng( YourClass.java: XX )
etc.. .
等等.. 。
回答by Summy
Check for inverse.containsKey(animal), BiMap<PhylogenyTree, Integer>
. The inverse might not have the animal.
检查inverse.containsKey(animal), BiMap<PhylogenyTree, Integer>
. 反过来可能没有动物。