Loading

NullPointerException de-reference a null object in Apex code trigger

Udgivelsesdato: May 17, 2026
Beskrivelse

When executing an Apex trigger, you may encounter the error:
"execution of [EventName] caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.[TriggerName]: line [XX], column [XX]"
A NullPointerException (NPE) in Apex occurs when code attempts to access a property or call a method on an object reference that has not been instantiated (is null), or when it tries to access a field on a record that was never populated with a value.
Common causes include:

  • A map lookup (e.g., accountMap.get(key)) returns null because the key does not exist in the map
  • A related record lookup returns null because the relationship field was not queried
  • A field value such as Site was left blank on the record, resulting in a null string

Note: If the field Site was left empty on the record, it will generate this error message as well.

 

Løsning

Resolve NullPointerException — Using a Null Check

To prevent a NullPointerException (NPE) in Apex, verify that each object and its attributes are not null before accessing them. In the example below, accountMap.get(oldAccount.Name) may return null if no matching Account exists in the map. The corrected code adds explicit null checks using if (newAccount != null) before accessing newAccount.Site, and a second check if (newAccount.Site != null) before calling .length() on the string field.

Account newAccount = accountMap.get(oldAccount.Name);

        if (newAccount != null) 

          if (newAccount.Site != null) 

            i = newAccount.Site.length();

The key pattern is: always check that both the parent object and its field are non-null before calling methods on them.
 

Exception Handling Routine — Using Try-Catch

Alternatively, you can use Apex's try-catch block to handle the NullPointerException gracefully at runtime. This approach allows your code to continue executing and display a user-friendly error message, rather than causing the entire transaction to fail.

Account newAccount = accountMap.get(oldAccount.Name);

        try {

             i = newAccount.Site.length();

            }
            catch (System.NullPointerException e) {

            e1 = e; // can be assigned to a variable to display a user-friendly error message

        }

In the example, the catch (System.NullPointerException e) block captures the exception and assigns it to a variable (e1) that can be used to display a descriptive error message to the user.

Best Practice

Best practice in Apex development is to always check for null values before accessing object fields or calling methods on objects that may not have been instantiated. This is especially important in triggers, where records may have optional fields left blank by users or by upstream processes. Using null checks is generally preferred over try-catch for performance-sensitive trigger code, as exception handling is slower than conditional checks.

Yderligere ressourcer
Vidensartikelnummer

000385601

 
Indlæser
Salesforce Help | Article