FIX - APEX Code Trigger NullPointerException de-reference a null object

FIX - APEX Code Trigger NullPointerException de-reference a null object


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.



Resolve NullPointerException

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();

Exception handling routine

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".

Knowledge Article Number


    • Related Articles

    • Copy Related Lists and Chatter on Lead Conversion

      In Salesforce, lead management allows your marketing and sales teams to market, qualify, and build your sales pipeline with prospective customers. Once qualified then a lead can be converted into a formal Account, Contact and (optionally) an ...
    • Deploying Process Builder Through Change Set

      We can understand process builder as a point-and-click apparatus that lets you effortlessly mechanize if/then trade forms and see a graphical representation of your handle as you construct. Each prepare comprises a trigger, at slightest one criteria ...
    • Deploying Workflows Through Change Set

      Salesforce Workflow Rules are the automated processes used in business processes to send Email alerts, assign a task, update a field on rule criteria or action based criteria requirements. Things to be considered when working with Workflow. ...
    • Improve performance and speed in Lightning Experience

      If you or your Users are experiencing slow page-loading times when using Lightning Experience, it may be related to one or more of the following issue types. Geographical Device Browser Salesforce organization configuration issues Please review issue ...
    • Best Practices - Salesforce Changesets

      What are the requirements for change sets utilization? Sending of these messages necessitates a deployment connection , which means that they can be sent only, like earlier mentioned, between ventures that are affiliated to each other. However, the ...