Explain Codes LogoExplain Codes Logo

Check if null Boolean is true results in exception

java
null-pointer-exception
boolean-checks
optional
Alex KataevbyAlex Kataev·Aug 11, 2024
TLDR
boolean isTrue = Boolean.TRUE.equals(yourNullableBoolean);

Safely check a nullable Boolean without raising a NullPointerException by comparing it with Boolean.TRUE. This method only returns true if the Boolean is both non-null and true.

Safe and smart Boolean checks

In Java, we often need to determine if a Boolean object is true. But, NullPointerException can spoil the party if the object is null.

Let's look at the savior code snippet that uses Boolean.TRUE.equals(myBool). It's safe because it calls equals() on Boolean.TRUE (never null), preventing NullPointerException. If myBool is null, it simply returns false.

// "NullPointerException, who's she?" - your code, probably boolean isTrue = Boolean.TRUE.equals(yourNullableBoolean);

Leveraging utility methods

External libraries like Apache Commons Lang offer handy solutions in the form of utility methods like BooleanUtils.isTrue(). It smoothly handles null values by returning false, thus preventing NullPointerException.

// Your code soon after using Apache Commons Lang library boolean isTrue = BooleanUtils.isTrue(yourNullableBoolean);

Opting for Java 8's Optional

For those on Java 8+, here's a refined and elegant syntax using java.util.Optional that gracefully handles null values.

// When Java 8 gives you Optionals, make null safety. boolean isTrue = Optional.ofNullable(yourNullableBoolean).orElse(false);

Grid out NullPointerException and safely handle nullable Boolean values.

Handling implicit auto-unboxing

Automatic unwrapping or auto-unboxing of Boolean objects to primitive boolean values by Java can lead to a NullPointerException.

Boolean.TRUE.equals() steps in to stop this by preventing auto-unboxing and null checks.

Streamlining with logical operators

Combining null checks with the condition prevents exceptions and makes your code cleaner:

if (yourNullableBoolean != null && yourNullableBoolean) { // "You may pass!" - Gandalf if he was a null check. }

The second half of the && operator is evaluated only when the Boolean is not null, thus preventing any exception.

Practical use cases

1. User access authentication:

// Checking if VIP or not-so-VIP Boolean hasAccess = user.getAccessFlag(); if (Boolean.TRUE.equals(hasAccess)) { // Roll out the red carpet }

2. Feature control:

// "To be or not to be enabled, that is the question." Boolean featureEnabled = configs.getFeatureFlag(); if (featureEnabled != null && featureEnabled) { // Fire up the engines and turn on the feature }

Handle edge cases effectively

Pay attention to scenarios where null Booleans can cause trouble and tackle them wisely:

  • Data serialization: Ensure null Boolean values are handled properly.
  • Database checks: Always sanitize your inputs to dodge those bullying exceptions.
  • Multi-threading: Safely publish Boolean objects across threads to avoid simultaneous NullPointerExceptions.