Skip to main content

Safe Navigation Operator

Explore practical guides and implementation patterns for this topic.

Posts

Topic Apex
Apex Development

How to Prevent & Handle Apex Null Pointer Exceptions: Safe Navigation & Best Practices

In plain words: A Null Pointer Exception (NPE) happens when your code tries to open an empty box. If a variable or object points to null (nothing) and you attempt to read its fields, call its methods, or get its list items, Apex crashes because there is no data in memory to work with. In Salesforce development, the System.NullPointerException is one of the most frequent runtime runtime errors. Because Apex is strongly typed but initializes unassigned object references to null , any attempt to traverse unpopulated relationships, empty map lookups, or unassigned variables will throw an unhandled exception. Writing defensive, null-safe Apex keeps your applications stable and prevents unexpected transaction rollbacks.
Read article
Topic Apex
Apex Development

How to Use the Safe Navigation Operator (?.) in Salesforce Apex

In plain words: The Safe Navigation Operator ( ?. ) in Salesforce Apex evaluates the left-hand side of an expression. If that value is null , it immediately returns null instead of throwing a dreaded System.NullPointerException (NPE). Null pointer exceptions are among the most common run-time errors in Apex development. Traditionally, guarding against them required writing layers of verbose if (variable != null) checks. The safe navigation operator streamlines your code by letting you chain references and method calls safely on a single line.
Read article