You are currently viewing Handle Null Pointer Exceptions in Salesforce

Handle Null Pointer Exceptions in Salesforce

A Null Pointer Exception in Salesforce—just like in other programming environments—happens when your code tries to use (or “dereference”) an object that hasn’t been properly initialized. In simple terms, it means you’re trying to access or perform actions on something that doesn’t actually exist.

In Salesforce, this kind of error typically shows up in Apex, the platform’s native programming language. Let’s take a look at some common scenarios where a Null Pointer Exception might occur in Salesforce:

  1. This happens when you declare an object but forget to initialize it. Since the object doesn’t actually point to anything, it’s considered null. So, if you try to access its properties or call a method on it, you’ll run into a Null Pointer Exception.
    • Account objAccount;
      System.debug(objAccount.Name);
  2. A Null Pointer Exception often happens when a method is expected to return an object but ends up returning null instead. If your code then tries to use that null object — for example, by calling one of its methods or accessing its properties — it will throw a Null Pointer Exception. This usually indicates that something went wrong earlier in the flow, like a failed lookup or an unexpected condition that wasn’t handled properly.
    • Account objAccount= [SELECT Id, Name FROM Account WHERE Name = 'Test' LIMIT 1];
    • System.debug(objAccount.Name); // Works only if record exists
  3. Method Returns Null: When a method is expected to return an object but instead returns null, trying to access any property or call a method on that null reference will lead to a Null Pointer Exception. This typically means the object wasn’t properly initialized or wasn’t found during execution.
    • public Account getAccountRecord(String id) {
      // Lets consider this method Return Null value
      return null;
      }
      Account objAccount= getAccountRecord('accountId');
      System.debug(objAccount.Name); // This will throw a Null Pointer Exception
  4. Dereferencing After Null Check: In some cases, you might check if a variable is not null, but by the time you actually use it, the variable has unexpectedly become null. This can happen due to asynchronous behavior or other logic updates in between. While traditional multithreading isn’t supported in Salesforce, similar issues can still arise in complex flows or when dealing with asynchronous operations like future methods, queueable jobs, or platform events.

Avoiding Null Pointer Exceptions: To prevent Null Pointer Exceptions, it’s important to make sure your object references aren’t null before using them. A straightforward way to handle this is by adding simple if-else checks to verify the object’s existence before accessing its properties or methods.

  1. if (myAccount != null) {
    System.debug(myAccount.Name);
    } else {
    System.debug('Account is null');
    }

 

Leave a Reply