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:
accountMap.get(key)) returns null because the key does not exist in the mapSite was left blank on the record, resulting in a null stringNote: If the field Site was left empty on the record, it will generate this error message as well.
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 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.
For more information on handling exceptions, check the Apex Language Reference, under "Using Exception Methods," or "Using Exception Variables."
000385601

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.