You need to sign in to do that
Don't have an account?

Potential Apex Code Bug: NullPointerException on simple if statement checking a boolean variable.
I'm getting a null pointer exception on a simple if check of a boolean variable. Here is the simplified version of the code that generates the exception:
Boolean boolVar = false;
if ( boolVar )
doSomething();
If I modify that and change "if ( boolVar )" to "if ( boolVar == true )" the null pointer exception goes away. This appears to be a bug in apex code. I can't reproduce this outside of my developer org or even with a simplified example in that org though. Here are one of the error messages I'm receiving:
-----------------------------
System.DmlException: Update failed. First exception on row 0 with id a078000000KSAKhAAP; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CRMfusionDBR101.DB_Scenario_Key_BIU: execution of BeforeUpdate
caused by: System.NullPointerException: Attempt to de-reference a null object
Class.CRMfusionDBR101.DB_Scenario.ProcessScenarioLinkUpdates: line 1405, column 1
Trigger.CRMfusionDBR101.DB_Scenario_Key_BIU: line 17, column 1: []
-------------------------
The line of code this points to is a line that just contains "else", nothing else.
However, if I change line 1507 in that file from:
if ( rowChanged )
to:
if ( rowChanged == true )
This error, and several others in my unit tests (some pointing to different lines) disappears. I can change back and forth to reproduce that issue. As far as apex code is concerned, both of those statements should be evaluated exactly the same. Even if the rowChanged variable was null it would still evaluate to false in an if check, not generate an exception.
Other exceptions in the unit tests, caused by this (again, all disappear with the simple change above):
caused by: System.NullPointerException: Attempt to de-reference a null object
Class.CRMfusionDBR101.DB_Scenario.ProcessScenarioLinkUpdates: line 1405, column 1
caused by: System.ListException: List index out of bounds: 1
Class.CRMfusionDBR101.DB_Scenario.ProcessScenarioLinkUpdates: line 1559, column 1
System.ListException: List index out of bounds: 1 |
| |
Stack Trace |
Class.CRMfusionDBR101.DB_Scenario.updateScenarioLinks: line 1327, column 1 | |
caused by: System.StringException: Invalid regex: Unclosed group near index 4
(\A
^
Class.CRMfusionDBR101.DB_Scenario.ProcessScenarioLinkUpdates: line 1452, column 1
caused by: System.NullPointerException: Attempt to de-reference a null object
Class.CRMfusionDBR101.DB_BuildKeyUpdates.buildKeyUpdates: line 342, column 1
Thanks,
Hi E.J.
I expect the simplified version actually works and there's something more to it. Especially if static variables and object fields are involved.
The key issue is - Booleans are generally not just true or false in Apex - they can be true, false or null. Uninitialized variables (especially static variables) and fields are where the problem is most common.
You'll see this especially if you create a new boolean field in an object or custom setting - even if the default value is false, it will be null until something actually sets it to false.
It's a real pain to deal with, and not really documented anywhere.
Dan
All Answers
Hi E.J.
I expect the simplified version actually works and there's something more to it. Especially if static variables and object fields are involved.
The key issue is - Booleans are generally not just true or false in Apex - they can be true, false or null. Uninitialized variables (especially static variables) and fields are where the problem is most common.
You'll see this especially if you create a new boolean field in an object or custom setting - even if the default value is false, it will be null until something actually sets it to false.
It's a real pain to deal with, and not really documented anywhere.
Dan
I'm aware that Booleans can be null in Apex Code. However, I was under the mistaken impression that Apex Code would evaluate a null to false, as most other languages I have worked with do (c, c++, c#, javascript, etc...). Apparently in this case I'm wrong. I came up with this easy to reproduce example to demostrate (wrongly) that a null value evaluates to false and it throws a NullPointerException:
Boolean testVal = null;
if ( testVal )
System.debug( 'Testval is true.' );
else
System.debug( 'Testval is false.' );
So, I stand corrected, thanks for getting me to check my assumptions. :) Typically I intialize all my variables anyway to prevent this kind of thing but given what I thought I knew about null evaluation in boolean comparisons I didn't do that in this particular method.
Thanks,
Hi E.J.
Your question and comments got me thinking and digging a bit further. I ended up writing the following blog article:
Fun with Booleans
Thanks for the inspiration.
Dan