Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown NullPointerException in Java

Tags:

java

The following snippet uses simple Java code.

package pkg;

final public class Main
{
    final private class Demo
    {
        private Integer value = null;

        public Integer getValue()
        {
            return value;
        }
    }

    private Integer operations()
    {
        Demo demo = new Demo();
        return demo==null?new Integer(1):demo.getValue();
    }

    public static void main(String[] args)
    {
        Main main=new Main();
        System.out.println("Value = " + String.valueOf(main.operations()));
    }
}

The above code works as expected with no problem and displays Value = null on the console.

In the following return statement,

return demo==null?new Integer(1):demo.getValue();

since the object demo of type Demo is not null, the expression after : which is demo.getValue() is executed which invokes getValue() within the inner Demo class which returns null and finally, it is converted to String and displayed on the console.

But when I modify the operations() method something like the one shown below,

private Integer operations()
{
    Demo demo = new Demo();    
    return demo==null?1:demo.getValue();
}

it throws NullPointerException. How?


I mean when I use this return statement

return demo==null?new Integer(1):demo.getValue();

it works (doesn't throw NullPointerException)

and when I use the following something similar return statement

return demo==null?1:demo.getValue();

it causes NullPointerException. Why?

like image 529
Lion Avatar asked Apr 17 '12 06:04

Lion


People also ask

What is NullPointerException in Java example?

NullPointerException is a runtime exception and it is thrown when the application try to use an object reference which has a null value. For example, using a method on a null reference.

Can NullPointerException be caught?

Programmers typically catch NullPointerException under three circumstances: The program contains a null pointer dereference. Catching the resulting exception was easier than fixing the underlying problem. The program explicitly throws a NullPointerException to signal an error condition.


2 Answers

This statement:

return demo==null?1:demo.getValue();

is actually being converted into something like this:

int tmp = demo == null ? 1 : demo.getValue().intValue();
return (Integer) tmp;

The type of the conditional expression is determined to be int (not Integer) based on the rules laid out in the JLS, section 15.25, using binary numeric promotion (5.6.2). The conversion from the null reference to int then triggers the NullPointerException.

like image 57
Jon Skeet Avatar answered Sep 23 '22 12:09

Jon Skeet


return demo==null?new Integer(1):demo.getValue();

In this case the type of the expression is Integer, and null is a legal value.

return demo==null?1:demo.getValue();

In this case the type of the expression is int, so your Integer return value is automatically unboxed. And as it is null, it throws a NPE.

like image 32
bongi Avatar answered Sep 22 '22 12:09

bongi