System.NullPointerException: Attempt to de-reference a null object

System.NullPointerException occurs when Apex code tries to access an object, variable, or field that has not been initialized or does not contain any data.

In simple terms, Salesforce is saying:

“You’re trying to use something that doesn’t exist yet.”

This error usually appears during:

  • Apex trigger execution
  • Apex classes and methods
  • Batch, Queueable, or Future Apex
  • Unit test execution
Common Error Message

System.NullPointerException: Attempt to de-reference a null object

This message is generic, which makes troubleshooting confusing—especially in large orgs.

Most Common Causes of System.NullPointerException

 

1. SOQL Query Returning No Records

If a query doesn’t return any data and the result is assigned directly to an sObject, Salesforce throws a null pointer exception.

Example

Account acc = [SELECT Id FROM Account WHERE Name = ‘Test’];

System.debug(acc.Id); // Fails if no record found

 

2. Missing Null Checks on Variables

Variables declared but never initialized will cause this error when accessed.

Example

String name;

System.debug(name.length()); // NullPointerException

 

3. Trigger Context Variables Are Null

Trigger variables like Trigger.new, Trigger.old, or Trigger.oldMap can be null depending on the trigger event.

Example

for (Account acc : Trigger.old) {

    // Fails in before insert

}

 

4. Related Records Are Not Loaded

Accessing related fields without querying them explicitly causes null reference issues.

Example

Contact c = [SELECT Id FROM Contact LIMIT 1];

System.debug(c.Account.Name); // Account not queried

 

5. Test Data Not Created in Test Classes

Many NullPointerExceptions occur only in test classes because required test data is missing.

How to Fix System.NullPointerException (Step-by-Step)
✅ Always Use Null Checks

if (acc != null) {

    System.debug(acc.Id);

}

 

✅ Query into Lists Instead of Single Records

List<Account> accList = [SELECT Id FROM Account WHERE Name = ‘Test’];

if (!accList.isEmpty()) {

    Account acc = accList[0];

}

 

✅ Validate Trigger Context

if (Trigger.isInsert && Trigger.new != null) {

    // Safe to proceed

}

 

✅ Query Required Related Fields

Contact c = [

    SELECT Id, Account.Name

    FROM Contact

    WHERE Id = :contactId

];

 

✅ Create Complete Test Data

Ensure your test classes create all required records, including related objects and mandatory fields.

How to Debug This Error Faster
  1. Enable Debug Logs for the affected user
  2. Look for the last executed line before failure
  3. Identify variables with null values
  4. Add System.debug() statements if needed
Best Practices to Prevent This Error
  • Always assume data may not exist
  • Never trust SOQL queries to return records
  • Avoid direct sObject assignments from queries
  • Bulk-safe code reduces unexpected nulls
  • Write defensive Apex, not optimistic Apex
Who Usually Encounters This Error?
  • 👨‍💻 Developers writing Apex logic
  • 🧠 Architects reviewing scalability issues
  • ⚙️ Admins when custom code breaks automation

Get the latest tips, news, updates, advice, inspiration, and more….

Need help fixing this in your org?

Our Salesofrce experts can debug and resolve this issue quickly.