English 中文(简体)
Java: null pointer exception when unboxing Integer?
原标题:

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.

最佳回答

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.

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.

问题回答

Check for inverse.containsKey(animal), BiMap<PhylogenyTree, Integer>. The inverse might not have the animal.

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).

node may be null and calling node.getParent.

The node s parent may be null and invoking parent.getChildren may throw an NPE.

One of the siblings may be null and invoking sibling.equals may throw an NPE.

cellInfo may be null and cellInfo.inverse will throw it.

Finally the "inverse" returned may be null and inverse.get() will throw it.

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.. .





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签