Beginner 15 min read

Null: The Concept of Nothing

By the end of this lesson, you'll be able to:

  • Explain what null represents
  • Distinguish null from a default or empty value
  • Write a basic null check before using a variable

Prerequisites: "A First Look at Non-Primitive Types"

Null is not the same as empty or zero

Integer count;              // null — never assigned
Integer zeroCount = 0;      // 0 — a real, assigned value
String name;                // null
String emptyName = '';      // '' — a real, assigned (empty) value
  1. Integer count; — declared but never assigned, so count is null — "no value," not zero.
  2. Integer zeroCount = 0; — this one is assigned, to the real value 0 — different from having no value at all.
  3. String name; — same idea for text: declared, never assigned, so name is null.
  4. String emptyName = ''; — assigned an empty string '' — a real value (just a very short one), not the same as null.

An uninitialized variable defaults to null — Apex's way of saying "this box exists, but nothing has been put in it yet." Zero and empty string are both real values; null is the absence of any value.

A real business example: Insurance

Decimal claimAmount; // not yet set — null
if (claimAmount != null) {
    System.debug('Claim amount: ' + claimAmount);
} else {
    System.debug('No claim amount recorded yet.');
}
  1. Decimal claimAmount; — no value assigned, so it's null.
  2. if (claimAmount != null) { — checks whether a value has actually been recorded before treating it as one.
  3. System.debug('Claim amount: ' + claimAmount); — runs only if line 2 is true.
  4. } else { — closes the if block and opens the alternative path, which runs only when line 2 is false.
  5. System.debug('No claim amount recorded yet.'); — the message shown when there's genuinely no amount yet — correctly distinct from claiming the amount is zero.
  6. } — closes the else block.

An insurance claim that hasn't been assessed yet genuinely has no amount — not zero, which would incorrectly suggest "assessed, and worth nothing." Null correctly represents "not yet known."

Common mistakes

  • Using a variable without checking for null first. Calling a method on a null variable (like .toUpperCase() on a null String) throws a NullPointerException — one of the single most common runtime errors in all of Apex.
  • Confusing null with zero in numeric logic. A null Decimal used in arithmetic will throw an error, not silently act like zero.

The classic null pointer mistake

String customerNotes;
System.debug(customerNotes.toUpperCase()); // throws NullPointerException!
  1. String customerNotes; — declared, never assigned — customerNotes is null.
  2. System.debug(customerNotes.toUpperCase()); — calling .toUpperCase() on a null value throws a NullPointerException immediately; there's no string there to uppercase.

Warning: calling a method on a variable that might be null will crash with a NullPointerException the moment that line runs. Always check if (variable != null) first when a value might not be set.

Exercise

Declare a String called middleName without assigning it a value. Write an if statement that debugs 'No middle name on file.' if it's null, or the name itself otherwise.

Show hint

if (middleName == null) { ... } else { ... }

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Null: The Concept of Nothing Quiz

1. What does null represent?

2. Calling a method on a null variable is always safe in Apex.

Log in to submit the quiz and save your score.

Leaderboard

  • 1. Monde Masiza 2/2

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Null means "no value at all" — not zero, not an empty string, but the complete absence of a value. Understanding null early prevents one of the most common runtime errors in Apex: the null pointer exception.