Use the safe navigation operator (?.) to replace explicit, sequential checks for null references. This operator short-circuits expressions that attempt to operate on a null value and returns null instead of throwing a NullPointerException.
Where possible, we changed noninclusive terms to align with our company value of Equality. We maintained certain terms to avoid any effect on customer implementations.
Important
If the left-hand-side of the chain expression evaluates to null, the right-hand-side isn’t evaluated. Use the safe navigation operator (?.) in method, variable, and property chaining. The part of the expression that isn’t evaluated can include variable references, method references, or array expressions.
All Apex types are implicitly nullable and can hold a null value returned from the operator.
Note
Examples
This example first evaluates a, and returns null if a is null. Otherwise the return value is a.b.
1a?.b // Evaluates to: a == null ? null : a.b
This example returns null if a[x] evaluates to null. If a[x] doesn’t evaluate to null and aMethod() returns null, then this expression throws a NullPointerException.
1a[x]?.aMethod().aField // Evaluates to null if a[x] == null
This example returns null if a[x].aMethod() evaluates to null.
1a[x].aMethod()?.aField
This example indicates that the type of the expression is the same whether the safe navigation operator is used in the expression or not.
1Integer x = anObject?.anIntegerField; // The expression is of type Integer because the field is of type Integer
This example shows a single statement replacing a block of code that checks for nulls.
1// New code using the safe navigation operator2String profileUrl = user.getProfileUrl()?.toExternalForm();
This example shows a single-row SOQL query using the safe navigation operator.
1// Previous code checking for nulls2results = [SELECT Name FROM Account WHERE Id = :accId WITH USER_MODE];3if(results.size() == 0){// Account was deleted4 return null;5}6return results[0].Name;
1// New code using the safe navigation operator2return[SELECT Name FROM Account WHERE Id = :accId WITH USER_MODE]?.Name;
Allowed use-case
Example
More information
Method or variable or parameter chains
aObject?.aMethod();
Can be used as a top-level statement.
Using parentheses, for example in a cast.
((T)a1?.b1)?.c1()
The operator skips the method chain up to the first closing parenthesis. By adding the operator after the parenthesis, the code safeguards the whole expression. If the operator is used elsewhere, and not after the parenthesis, the whole cast expression isn’t be safeguarded. For an example, see Incorrect use of safe navigation operator with cast.
SObject chaining
String s = contact.Account?.BillingCity;
An SObject expression evaluates to null when the relationship is null. The behavior is equivalent to String s = contact.Account.BillingCity.
SOQL Queries
String s = [SELECT LastName FROM Contact]?.LastName;
SOQL query with safe navigation operator: behavior when no records returned
1List<Contact>contacts = [SELECT LastName FROM Contact WITH USER_MODE];2String s;3if(contacts.size() == 0){4 s = null; // New behavior when using Safe Navigation. Earlier, this would throw an exception.5}else if(contacts.size() == 1){6 s = contacts.get(0).LastName;7}else{8 // contacts.size() > 1 throw new QueryException(...);9}
You can’t use the Safe Navigation Operator in certain cases. Attempting to use the operator in these ways causes an error during compilation:
Types and static expressions with dots. For example:
Namespaces
{Namespace}.{Class}
Trigger.new
Flow.interview.{flowName}
{Type}.class
Static variable access, method calls, and expressions. For example:
AClass.AStaticMethodCall()
AClass.AStaticVariable
String.format('{0}', 'hello world')
Page.{pageName}
Assignable expressions. For example:
foo?.bar = 42;
++foo?.bar;
SOQL bind expressions. For example:
1public with sharing class X{2 public String query = 'xyz';3}4X x = new X();5List<Account>accounts = [SELECT Name FROM Account WHERE Name = :X?.query WITH USER_MODE]6List<List<SObject>>moreAccounts = [FIND :X?.query IN ALL FIELDS7 RETURNING Account(Name)];
With addError() on SObject scalar fields. For example:
1Contact c;2c.LastName?.addError('The field must have a value');
You can use the operator with addError() on SObjects, including lookup and master-detail fields.