Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException from Boolean

This is one for the java purists I think. I recently had an issue with a method to perform a custom parsing of String values to a Boolean. A simple enough task, but for some reason the method below was throwing a NullPointerException in the null case...

static Boolean parseBoolean(String s)
{
    return ("1".equals(s) ? true : ("0".equals(s) ? false : null));
}

The return type for the method is Boolean so why or how can a NullPointerException be thrown? From debugging through it seems the exception is being thrown at the point where the nested in-line conditional statement evaluates to null and returns null to the outer in-line conditional, but again I can't explain why.

Eventually I gave up and rewrote the method as follows, which works as expected:

static Boolean parseBoolean(String s)
{
    if ("1".equals(s)) return true;
    if ("0".equals(s)) return false;

    return null;
}

The following code is half way between the two and also works as expected:

static Boolean parseBoolean(String s)
{
    if ("1".equals(s)) return true;

    return "0".equals(s) ? false : null;
}
like image 705
Robin Avatar asked Apr 26 '13 09:04

Robin


People also ask

How do you do null check for Boolean?

You can use the class Boolean instead if you want to use null values. Boolean is a reference type, that's the reason you can assign null to a Boolean "variable". Example: Boolean testvar = null; if (testvar == null) { ...}

Can Boolean values null?

Nullable boolean can be null, or having a value “true” or “false”. Before accessing the value, we should verify if the variable is null or not. This can be done with the classical check : if … else …

How do you fix a 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.

How do you know if a Boolean is real?

To check if a value is of boolean type, check if the value is equal to false or equal to true , e.g. if (variable === true || variable === false) . Boolean values can only be true and false , so if either condition is met, the value has a type of boolean. Copied!


2 Answers

This also works:

static Boolean parseBoolean(String s)
{
    return ("1".equals(s) ? Boolean.TRUE : ("0".equals(s) ? Boolean.FALSE : null));
}

So the reason you get an NPE is due to autoboxing because using boolean in the ternary operator causes the result of the expression to be treated as a boolean. And un-boxing of null causes an NPE.

like image 115
NilsH Avatar answered Oct 22 '22 11:10

NilsH


My suggestion? Don't return Boolean, return boolean and throw an exception:

static boolean parseBoolean(String s)
{
  if ("1".equals(s)) return true;
  if ("0".equals(s)) return false;

  throw new IllegalArgumentException(s + " is not a boolean value.");
}

Adopting an approach like the above will help avoid you accidentally referencing a null Boolean object.

See the excellent answer from NilsH to see why your original method is throwing an exception.

like image 31
Duncan Jones Avatar answered Oct 22 '22 12:10

Duncan Jones