I noticed an issue with java.lang.Boolean class that it can not parse nulls. I know it has the parseBoolean static method but as it's signature states it only accepts String and not an Object.
In other words, it has the following signature:
public static boolean parseBoolean(String s)
but not:
Boolean.parseBoolean(Object)
What is the best way to check a Boolean value without falling on NullPointerException?
Try that approach:
Boolean.TRUE.equals(yourObj);
If you want your parse to return true, false or null as a Boolean object, take a look at Apache Commons Lang. BooleanUtils has a one liner that does exactly this.
https://commons.apache.org/proper/commons-lang/javadocs/api-2.4/org/apache/commons/lang/BooleanUtils.html#toBooleanObject(java.lang.String)
BooleanUtils.toBooleanObject(null) == null
BooleanUtils.toBooleanObject("true") == true
BooleanUtils.toBooleanObject("false") == false
BooleanUtils.toBooleanObject("YES") == true
BooleanUtils.toBooleanObject("nO") == false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With