21

I ran across a very weird NPE this morning, and reduced it to a simple example. Is this a JVM bug or correct behavior?

public class Test1 {
    class Item {
        Integer id = null;
        public Integer getId() {return id;}
    }   
    public Integer f() {
        Item item = new Item();
        // this works:
        //return item == null ? new Integer(1) : item.getId();

        // NPE??
        return item == null ? 1 : item.getId();
    }   
    public static void main(String[] args) {
        Test1 t = new Test1();
        System.out.println("id is: " + String.valueOf(t.f()));
    }   
}

Output from compile and run:

$ javac Test1.java 
$ java Test1
Exception in thread "main" java.lang.NullPointerException
at Test1.f(Test1.java:12)
at Test1.main(Test1.java:16)
$
Kevin
  • 24,871
  • 19
  • 102
  • 158
  • 1
    pst use `Integer.valueOf(1)` instead of `new Integer(1)` – ratchet freak Oct 18 '11 at 18:04
  • 1
    that'a a good catch with autoboxing. This is why popular best practices recommend to use primitive type over wrapper. – Cygnusx1 Oct 18 '11 at 18:15
  • 1
    Very similar to [strange Java NullPointerException with autoboxing](http://stackoverflow.com/questions/3265948/strange-java-nullpointerexception-with-autoboxing) – Pops Apr 30 '12 at 17:14

5 Answers5

34

The type of the expression item == null ? 1 : item.getId() is int not Integer. Therefore, Java needs to auto-unbox your Integer to an int (causing the NullPointerException). Then it auto-boxes the result back to an Integer (well it would if not for the NullPointerException) to return from the method.

On the other hand, the expression item == null ? new Integer(1) : item.getId() has a type of Integer and no auto-unboxing needs to be done.

When you auto-unbox a null Integer, you get a NullPointerException (see Autoboxing) and that is what you are experiencing.

To answer your question, this is correct behavior.

Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77
3

item may not be null, but when you call getId(), that is returning null. When you try to auto-unbox null, you get an NPE.

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • Are you sure? getId returns an Integer that happens to be null, so there should be no autoboxing. And if what you say is true then why does returning `new Integer(1)` make the NPE go away? – Kevin Oct 18 '11 at 18:05
  • 6
    @Kevin With `new Integer(1)`, both the 2nd and 3rd operands are of type `Integer`, so that's the type of the conditional. With `1`, the type of the conditional expression is `int`, so `getId()`'s return value is unboxed to an `int`. (Check 15.25 of the JLS for the exact rules around this.) – dlev Oct 18 '11 at 18:07
3

If you decompile the class file you will see clearly your NPE...

return Integer.valueOf(item != null ? item.getId().intValue() : 1);
smp7d
  • 4,947
  • 2
  • 26
  • 48
2

The return type below is Integer -

public Integer f() {
    Item item = new Item();
    // this works:
    //return item == null ? new Integer(1) : item.getId();

    // NPE??
    return item == null ? 1 : item.getId();
}

And the result of the following -

item == null ? 1 : item.getId()

is null in your case.

So, JVM is throwing NPE because it is trying to autobox null.

Try -

new Integer(null); // and
Integer.valueOf(null);

both will throw NPE.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
2

It happens because you are using conditional operator ?. Line

return item == null ? 1 : item.getId();

is equivalent to

int result = item == null ? 1 : item.getId();
return result;

The result is int because of the first operand in your expression. This is the reason that your code works when you explicitly wrap 1 with Integer. In this case the compiler creates something like

Integer result = item == null ? new Integer(1) : item.getId();
return result;

So, NPE happens when attempting to "cast" item.getId() (that is null) to int.

AlexR
  • 114,158
  • 16
  • 130
  • 208