Safe Navigation Operator – Avoid Null Pointer Exceptions

In this post, we will use a new feature introduced in Winter ’21 that is Safe Navigation Operator in Salesforce (Apex) to replace explicit, sequential checks for null references and unnecessary IF conditions. This Operator is useful to avoid Null Pointer Exceptions in Apex. It is a Short-Circuit operator that attempts to operate on a null value and returns null instead of throwing a NullPointerException in Apex.

How Safe Navigation Operator works?

Safe Navigation Operator (?.) first evaluates the Left-Hand-Side of the chain expression. If it is NULL, the Right-Hand-Side is evaluated and returns NULL. We can use this operator in variable, method, and property chaining.

To use the Safe Navigation Operator, we just need to replace the dot (.) with “?.“. For Example, to access the Subject of Case, we use Case.Subject. This can be replaced with Case?.Subject.

How to avoid Null Pointer Exceptions?

Example 1

Consider below code snippet:

Case objCase; // Creating a NULL reference.
System.debug(objCase.Subject); // Throws Null Pointer Exception.
System.debug(objCase?.Subject); // Prints NULL.

Here, objCase is NULL. Hence Line 2 will throw a Null Pointer Exception. We can get rid of NullPointerException by putting the IF condition and check if objCase is not NULL. But that is unnecessary.

If we replace objCase.Subject with objCase?.Subject just like in Line 3, it will print NULL in debug logs instead of throwing that NullPointerException in our face.

Example 2

Safe Navigation Operator can also be used with SOQL queries. Consider another example. We have the Id of the Case and we want to print the Description of that Case. For this simple requirement, we need to write the below lines of code.

list<Case> lstCase = [SELECT Description FROM Case WHERE Id='500B0000006lGAhIAM'];
if(lstCase != null && !lstCase.isEmpty()){

Here, we need the IF condition to check if the lstCase is Empty or NULL. Because if no record is returned, it will throw the List Index Out of Bounds Exception.

With Safe Navigation Operator, all 4 lines of code can be replaced with single line:

System.debug([SELECT Description FROM Case WHERE Id='500B0000006lGAhIAM' LIMIT 1]?.Description);

As the SOQL will only return 1 record, we can directly use “?.” with the SOQL query.

Example 3

Safe Navigation Operator can be used with any Type in Salesforce that can have NULL values. So it can be used with Strings as well. In addition to that, it can be used with methods as well. Consider below example:

String str = 'niks developer';

We need to put the IF condition here to check if the str is Empty or NULL. With Safe Navigation Operator, this can be achieved with a single line.

String str = 'niks developer';

That is all from this post. If you don’t want to miss a new post, please Subscribe here.

You can check the implementation for another new feature that is introduced in WInter ’21 that is Send Custom Notification using Apex in Salesforce.

If you want to know more about Safe Navigation Operator in Apex, you can check official Salesforce documentation here.

See you in the next post!

Leave a Comment