16

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));


    }
}
4
  • One reason to avoid using Boolean (or other wrapper class types) if at all possible, or if you must use it, test it for null before using it. Commented Sep 13, 2014 at 14:43
  • @HovercraftFullOfEels This is our friend from a few minutes ago, testing out the solution to his assignment question :) Commented Sep 13, 2014 at 14:44
  • @MarkoTopolnik: indeed. 1+ to your answer. Commented Sep 13, 2014 at 14:54
  • possible duplicate of Why comparing Integer with int can throw NullPointerException in Java? Commented Sep 14, 2014 at 6:13

5 Answers 5

47

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";
Sign up to request clarification or add additional context in comments.

4 Comments

Great trick with the Boolean statics and methods, it simplifies code so much! +1
This also works: import static java.lang.Boolean.TRUE; if (param == TRUE) {}
@sorin7486 It doesn't work in general: new Boolean(true) != Boolean.TRUE.
@MarkoTopolnik you are correct.. have I mentioned I don't like java? :)
1

Your code compares a java.lang.Boolean instance with a primitive boolean, which means unboxing the java.lang.Boolean. Since null can't be unboxed, a NullPointerException is thrown.

You could work around this by using the built in constants Boolean.TRUE and Boolean.FALSE:

public static String bool(Boolean param) {
    if (Boolean.TRUE.equals(param)) {
        return "a";
    } else if (Boolean.FALSE.equals(param)) {
        return "b";
    }
    return "c";
}

1 Comment

Great trick with the Boolean statics and methods, it simplifies code so much! +1
0

You used the Boolean instead of boolean. Boolean is a class, which means you can assign objects to it. In your case, you passed in a null, which is then assigned to param. You then tried to use param, which of course resulted in a NullPointerException.

You can:

  • Get rid of the line bool(null)
  • Change Boolean to boolean in the parameters for bool()
  • Add an else if for when param is null

Comments

0

So your program must be something like this.

public class BooleanBug {

    public static String bool(Boolean param) {
        if ((null != param) && param.booleanValue() == true) {
            return "a";
        } else if ((null != param) && param.booleanValue() == false) {
            return "b";
        }
        return "c";

    }

    public static void main(String[] args) {

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

    }
}

Comments

0

Clearly, as mentioned by others if param is null, the below throws NullPointerException.

param == true

According to the Java language specification, unboxing happens via calling Boolean.booleanValue(), Number.longValue(), Number.intValue() etc. Its like calling those methods manually and NullPointerException is the result of unboxing a null.

We can also detect these Unboxing vulnerabilities, if not handled properly using static code analysis tools. Kindly refer this for more information Boolean Violation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.