Question

In Java, you would usually say that

if(someBool != false)

is the same as

if(someBool)

But what if someBool is not of type boolean but Boolean, and its value is null?

Was it helpful?

Solution

It will throw a NullPointerException (autounboxing of null throws NPE).

But that only means that you must not allow a null value. Either use a default, or don't use autounboxing and make a non-null check. Because using a null value of a boolean means you have 3, not 2 values. (Better ways of handling it were proposed by Michael and Tobiask)

OTHER TIPS

If you want to handle Boolean instances as well as primitives and be null-safe, you can use this:

if(Boolean.TRUE.equals(someBool))

Use ApacheCommons BooleanUtils.isTrue() or .isFalse()

If someBool is Boolean

if (someBull != null && someBull) {
  //Yeah, true.
}

Since Boolean can be null make sure you avoid NullPointerException by checking for not null.

I did a little test:

    Boolean o = null;
    try {
        System.out.println(o ? "yes" : "no");
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        System.out.println((o != false) ? "yes" : "no");
    } catch (Exception e) {
        e.printStackTrace();
    }

The output is surprising:

java.lang.NullPointerException
    at btest.main(btest.java:10)
java.lang.NullPointerException
    at btest.main(btest.java:15)

The first NPE is to be expected, because o will be autounboxed (and that fails because it's null). The second happens for the same reason, but it doesn't feel natural. Anyway, the solution is to do:

System.out.println(!Boolean.FALSE.equals(o) ? "yes" : "no");

You can however compare a null Boolean with a Boolean instance. For example :

Boolean myBool = null;
System.out.println(myBool == Boolean.FALSE);
System.out.println(myBool == Boolean.TRUE);

prints :

false
false

Good illustrations of the difference between the primitive boolean & the object Boolean. The former can be only true or false. The latter can be true, false, or unknown/undefined. (i.e., null). Which you use depends on whether you want to deal with two use cases or three.

It's old, but Boolean.valueOf(null) is false, just like Boolean.valueOf(false) is false.

Actually the Boolean constructor accepts null, returns FALSE and doesn't throw a NullPointerTantrum.

 new Boolean(null);
 <false>

This has the added bonus of also giving a thruthy response to the string "true" which is not the case for Boolean.TRUE.equals but we are more restricted again having only constructors for Strings and Booleans.

Something you can overcome with string concatenation, which is also null-proof.

 new Boolean(""+null);
 <false>

 new Boolean(""+false);
 <false>

 new Boolean(""+new Object());
 <false>

 new Boolean(""+6);
 <false>

 new Boolean(""+new Integer(9));
 <false>

Ensuring that all the TRUE options, available in java, still remains.

 new Boolean(""+true);
 <true>

 new Boolean(""+"true");
 <true>

If it's Java 7+ you can use

import java.util.Objects;

And

if (Objects.equals(someBool, true))

As Boolean will give you an object, you must always check for NULL before working on the object

If its null then you'll get a NullPointerException

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top