Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NullPointerException with boolean

Tags:

java

methods

I wrote a realy simple code based on another question and here it is:

It throws me an error

java.lang.NullPointerException line 5 and 17

I don't know what I'm doing wrong.

 public class Main {

    public static String bool(Boolean param){
        if(param == true){    (line 5)
            return "a";
        }else if(param == false){
            return "b";
        }
        return "c";

    }

    public static void main(String[] args){

        System.out.println(bool(true));
        System.out.println(bool(null)); (line 17)
        System.out.println(bool(false));


    }
}
like image 774
Alejandro Garcia Avatar asked Sep 13 '14 14:09

Alejandro Garcia


People also ask

How do I resolve Java Lang NullPointerException?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

Can we assign null to boolean in Java?

A boolean cannot be null in java. A Boolean , however, can be null . If a boolean is not assigned a value (say a member of a class) then it will be false by default.

What causes a Java Lang NullPointerException?

What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.

Can you catch NullPointerException in Java?

It is generally a bad practice to catch NullPointerException. 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.

What is NullPointerException in Java?

It essentially means that object reference variable is not pointing anywhere and refers to nothing or ‘ null ‘. A example java program which throws null pointer exception. 2. Common places where Java NullPointerException usually occur

What is a Boolean class in Java?

Java.lang.Boolean Class in Java. The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field, whose type is boolean. In addition, this class provides useful methods like to convert a boolean to a String and a String to a boolean, while dealing with a boolean variable.

How does the compiler interpret null as a Boolean?

The compiler interprets null as a null reference to Boolean, applies the autoboxing/unboxing rules for Boolean (on a null) => you get a NullPointerException at runtime. @NilsH probably at the time of writing missed this part of yours. :) +1 from me.

What can you do with a null object in Java?

Accessing or modifying a field or data member of the null object. Passing null object as an argument to a method. Calculating the length of a null array. Accessing index of a null array. Synchronizing a null object. Throwing a null object. The Null Pointer Exception extends from the class RuntimeException.


1 Answers

null cannot be auto-unboxed to a primitive boolean value, which is what happens when you try to compare it with true. In

param == true

The type of true is boolean, therefore the left-hand operand must also be a boolean. You are passing in a Boolean, which is an object, but can be auto-unboxed to boolean.

Therefore this is equivalent to

param.booleanValue() == true

Clearly, if param is null, the above throws NullPointerException.

To avoid the hidden pitfalls of auto-unboxing, you could instead work with the Boolean objects:

if (Boolean.TRUE.equals(param))
  return "a";
if (Boolean.FALSE.equals(param))
  return "b";
return "c";
like image 113
Marko Topolnik Avatar answered Oct 03 '22 01:10

Marko Topolnik