RP. ← Back to Blog
Development January 12, 2026

The Silent Crasher: Understanding null in Apex

Every Salesforce Developer has seen it: System.NullPointerException. Let's break down why this happens and how to stop it.

1. What is a "Null" value?

In Apex, null is a literal that represents a variable that exists but has **no value assigned**. It is a point in memory that leads to nowhere.

Account myAcc; // myAcc is currently null
System.debug(myAcc); // Output: null

2. The "De-reference" Error

The most common error is the **NullPointerException (NPE)**. This occurs when you try to access a field or method on a variable that is null.

Data Type Behavior if Null
String Different from empty string "".
Decimal Cannot be used in math without a check.
SObject Fields return null if record is not found.

How to Code Safely:

Safe Navigation Operator (?.)

String name = myAccount?.Name;

Stops the crash and returns null instead of an error.

The Standard Null Check

if (myList != null && !myList.isEmpty()) { ... }

Always verify data exists before looping.

Real-World Scenarios

Compare the "Dangerous" approach with the "Professional" solution.

Danger

⚠️ The SOQL Trap

// This crashes if no record is found
Account a = [SELECT Id FROM Account LIMIT 0];
System.debug(a.Id); // ❌ NPE Error!
Safe

✅ The Defensive List

// Use a list to prevent crashes
List<Account> accs = [SELECT Id...];

if (!accs.isEmpty()) {
  System.debug(accs[0].Id);
}

The 2026 Way: Safe Navigation Operator

Instead of writing long if/else chains to check for nulls, use the Safe Navigation Operator (?.). It stops the execution if the variable is null and returns null instead of throwing a crash.

Clean Code Apex 60.0
// Old Way (4 lines)
if (con != null) { name = con.Name; }

// New Way (1 line)
String name = con?.Name;

Declaration vs. Instantiation

In Apex, simply declaring a List is not the same as creating one. Until you use the new keyword, your variable is just a label pointing to "nothing".

The Issue

Just Declared

// Variable is declared but is null
List<Integer> myFavNumbers;

if(myFavNumbers != null) {
  // ❌ This code is skipped
} else {
  System.debug('You have not decided...');
}
The Fix

Instantiated

// List is now a real object []
List<Integer> myFavNumbers = new List<Integer>();

if(myFavNumbers != null) {
  // ✅ This code now runs
  System.debug('List exists!');
}

The Restaurant Analogy 🍽️

Null (Declared)

You have a reservation for a table, but the restaurant hasn't actually set the table yet. There is no physical place to sit.

Empty (Instantiated)

The table is set and ready. Even if no one is sitting there yet, the space is allocated and ready for guests (data).

Empty Strings vs. Null: The Great Illusion

In your provided code, you might expect the "No Site" debug to run, but it won't. Here is why '' is not the same as null.

The Empty String ('')

acc.Site = '';
if(acc.Site == null) {
  // ❌ Skipped
}

Result: "Site has been published"

The Actual Null

acc.Site = null;
if(acc.Site == null) {
  System.debug('NO Site has been published');// ✅ This runs
}

Result: "NO Site has been published"

🚀 Professional Best Practice: String.isBlank()

To catch both null AND '', Salesforce developers use the String.isBlank() method.

if(String.isBlank(acc.Site)) {
  System.debug('Truly empty or null!');
}

Test Your Knowledge 🧠

Can you spot the Null Pointer before it crashes the system?

SCENARIO 01

The Math Trap

You are incrementing a counter on an Account. If the field Total_Contacts__c is currently empty (null), what happens?

myAcc.Total_Contacts__c += 1;
SCENARIO 02

The SOQL Lookup

You query for a single Lead by email. What happens if no Lead matches that email?

Lead myLead = [SELECT Id FROM Lead WHERE Email = :input LIMIT 1];