Apex
Research draft · Response not yet published
Why can an Apex condition still fail after using safe navigation?
A short expression uses the safe navigation operator on a null String, yet the surrounding Boolean condition throws a null pointer exception.
This page is a starting point for my own investigation. It does not yet provide a tested solution.
Read the original community question ↗Already answered. Reproduce the behavior and explain the result in your own words rather than repeating the existing answer.
What I’ll investigate
- Run the smallest example with null, an empty String, a matching value, and a non-matching value.
- Inspect the intermediate Boolean result before writing the final condition.
Research notes
Reading notes checked on 5 December 2026. Org testing is still pending.
Reading note: Salesforce documents that safe navigation returns null when its left operand is null. It does not promise a false Boolean result. Investigate the difference between safely evaluating a method call and using its result in a condition. Not yet tested in an org.
My response
I haven’t written my response yet. After testing, I’ll explain the cause, the smallest fix, and how I verified it, then credit the original question and any answers I build on.
Solution or simplified workaround
Pending validation. I’ll explain when to use it, the smallest steps, the expected result, limitations, and how to undo it.
Validation
Not yet tested. I’ll record the actual results after reproducing the issue.
- Null input does not throw in the revised example.
- Matching and non-matching inputs take the intended branches.
- Assertions cover the four input cases.
Possible next step
A small Apex tip with an assert-based example; no library is needed.