Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException in method returning type Object [duplicate]

Why does the following throw a NullPointerException? :

public static Object myTest() { 
    boolean x = false; 
    boolean y = false; 
    return x && y ? new Object() : x ? x : y ? y : null;
}

public static void main(String [ ] args) {
    myTest(); 
}

I know if i do either of the following the code will not throw a NullPointerException:

A)

public static Object myTest() { 
    boolean x = false; 
    boolean y = false; 
    return x && y ? new Object() : x ? x : y ? y : (Object) null;
}

public static void main(String [ ] args) {
    myTest(); 
}

B)

public static Object myTest() { 
    Boolean x = false; 
    Boolean y = false; 
    return x && y ? new Object() : x ? x : y ? y : null;
}

public static void main(String [ ] args) {
    myTest(); 
}

Also, if i change the code completely and do the following it works:

public static Object myTest() { 
    boolean x = false; 
    boolean y = false; 

    if(x && y) {
        return new Object(); 
    } else if(x) {
        return x; 
    } else if(y) {
        return y;
    } else {
        return null;
    }
}

public static void main(String [ ] args) {
    myTest(); 
}

I guess the compiler is doing some sort of an optimization and somehow messing things up? I'm assuming it's some sort of a casting problem, but why would it throw a NullPointerException in that case instead of a ClassCastException? Any information as to why this is happening would be greatly appreciated!

Thanks in advance

like image 587
BigBug Avatar asked Jan 27 '15 16:01

BigBug


People also ask

Why do I get NullPointerException when method parameters are null?

According to the Javadoc for NullPointerException, NullPointerException is meant to be used for attempting to use null where an object is required. If our method parameter isn't intended to be null, then we could reasonably consider this as an object being required and throw the NullPointerException.

What is a null pointer exception?

374 A null pointer exception is thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a nullobject.

Why do I get NullPointerException when I type when-thenreturn?

So instead of when-thenReturn , you might type just when-then. Maybe it was IntelliSense. Maybe you did it accidentally. But for sure, NullPointerException happened because you want something which is not there. Debug and check if you are returning something.

Is it better to avoid returning null objects in methods?

6 IMO, it is preferable to avoid returning null objects in methods when possible and use annotation when null input parameters are not allowed in order to, by contract, reduce the amount of ´if (obj==null)´ in the code and improve the code readability. – L. G. Feb 27 '17 at 9:43


1 Answers

If you add some parenthesis for readability:

return (x && y) ? (new Object()) : (x ? x : (y ? y : null));

You can see that at y ? y : null, the compiler will attempt to unbox null (so that the types match), causing a NPE to be thrown.

like image 110
August Avatar answered Oct 13 '22 13:10

August