Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java NPE in ternary operator with autoboxing?

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)
$
like image 397
Kevin Avatar asked Oct 18 '11 17:10

Kevin


People also ask

Can I throw exception in ternary operator Java?

You can't throw an exception in a ternary clause. Both options must return a value, which throw new Exception(); doesn't satisfy.

Can we use ternary operator in if condition in Java?

Java ternary operator is the only conditional operator that takes three operands. It's a one-liner replacement for the if-then-else statement and is used a lot in Java programming. We can use the ternary operator in place of if-else conditions or even switch conditions using nested ternary operators.

What is ternary operator in Java with example?

Ternary Operator in Java A ternary operator evaluates the test condition and executes a block of code based on the result of the condition. if condition is true , expression1 is executed. And, if condition is false , expression2 is executed.


5 Answers

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.

like image 110
Jack Edmonds Avatar answered Oct 07 '22 23:10

Jack Edmonds


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

return Integer.valueOf(item != null ? item.getId().intValue() : 1);
like image 30
smp7d Avatar answered Oct 07 '22 22:10

smp7d


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.

like image 32
nicholas.hauschild Avatar answered Oct 08 '22 00:10

nicholas.hauschild


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.

like image 23
Bhesh Gurung Avatar answered Oct 08 '22 00:10

Bhesh Gurung


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.

like image 41
AlexR Avatar answered Oct 07 '22 23:10

AlexR