When executing Salesforce Apex code, you may encounter an error called "Attempt to de-reference a null object." This error occurs when you attempt to reference a method or property on a null object.
In this article, we will explain the cause of this error and the appropriate countermeasures in detail.
1. Overview of the errorError message: "System.NullPointerException: Attempt to de-reference a null object"
Cause: You attempted to access a method or property of a variable while that variable was null.
Impact: The execution of the Apex code fails, and the process stops midway.
Typical error cases
Cause Example A SOQL query returns 0 results, and the object becomes null Account acc = [SELECT Id, Name FROM Account WHERE Name = 'NonExistent' LIMIT 1]; Attempting to retrieve an element from an empty list Account acc = accList[0]; (accList is empty) Attempting to apply a method to a null object acc.Name = 'Updated Name'; (acc is null)
2. Main causes of the error① SOQL query fails to retrieve data, and it is used while still null
NG Example (null reference when there is no data)
Account acc = [SELECT Id, Name FROM Account WHERE Name = 'NonExistent' LIMIT 1]; System.debug(acc.Name); // レコードがないと acc は null になり、エラー発生
② Accessing an element while the list is empty
NG Example (referencing an element when the list is empty)
List<Account> accList = new List<Account>(); Account acc = accList[0]; // リストが空のためエラー
③ Calling a method on a null object
NG Example (applying a method to a null object)
Account acc = null; System.debug(acc.getName()); // acc が null のためエラー
① Check the results of the SOQL query
Corrected code (confirming that the query result is not empty)
List<Account> accList = [SELECT Id, Name FROM Account WHERE Name = 'NonExistent' LIMIT 1];
if (!accList.isEmpty())
Account acc = accList[0];
System.debug(acc.Name);
else
System.debug('No matching records found.');
Improvement: Confirm that the query result is not empty using isEmpty().
② Check the number of elements in the list
Corrected code (check that the list is not empty)
List<Account> accList = [SELECT Id, Name FROM Account LIMIT 1];
if (!accList.isEmpty())
Account acc = accList[0];
System.debug(acc.Name);
else
System.debug('List is empty');
Improvement: Use isEmpty() to check the contents of the list.
③ Perform a null check
Corrected code (perform a null check)
Account acc = null;
if (acc != null)
System.debug(acc.Name);
else
System.debug('Account is null');
Improvement: Perform a null check with if (acc != null) to execute the process safely.
④ Utilize try-catch
Corrected code (implement exception handling with try-catch)
try
Account acc = null;
System.debug(acc.Name);
catch (NullPointerException e)
System.debug('NullPointerException occurred: ' + e.getMessage());
Improvement: Use try-catch to ensure the process does not stop even if an error occurs.
The "Attempt to de-reference a null object" error occurs when you try to reference a method or property on a null object. You can avoid this error by checking SOQL results, verifying list elements, performing null checks, and utilizing try-catch.
If you want to learn more about Salesforce, Udemy courses are recommended!
Udemy coupon link
I have courses ranging from administrator settings to Flows and Apex, so if you want to learn more, please take a look at the overview at least once.
You can also consult via our website
https://effinous.com/
By properly managing Salesforce data processing and avoiding errors, let's improve the stability and efficiency of your work!