The execution of a trigger will sometimes generate an error message, "execution of [EventName] caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.[TriggerName]: line [XX], column [XX]"
This error is caused by a line of code that is trying to use an object that has not been instantiated, or an object's attribute that has not been initialized.
NOTE: If the field Site was left empty, it will generate the error message as well.
The solution is to make sure the Object and/or the Attribute to be used is not null. In this example, the code needs to be modified as follows:
Account newAccount = accountMap.get(oldAccount.Name);
if (newAccount != null)
if (newAccount.Site != null)
i = newAccount.Site.length();
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
}
For more information on handling exceptions, check the Apex Language Reference, under "Using Exception Methods," or "Using Exception Variables."
For more information on initializing variables, look under Chapter 2, "Language Constructs", in the topics "Variables", and "Case Sensitivity".
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.