Null-safe string handling in Java
"literal".equals(x), Objects.equals, isEmpty vs isBlank.
NullPointerException. The culprit is a method call on... nothing.null means "no object"
A null reference points to no object at all. Calling any method on it — equals, isEmpty, length — throws NullPointerException, because there's nobody there to answer.
String answer = null;
answer.equals("yes"); // NPE!Who should go first?
if (answer.equals("yes")) {
...
}If answer is null, this throws.
if ("yes".equals(answer)) {
...
}The literal is never null, and equals(null) simply returns false.
Objects.equals: safe on both sides
When both values might be null, use Objects.equals(a, b). It returns true if both are null, false if only one is, and otherwise calls a.equals(b).
Objects.equals(null, null); // true
Objects.equals("a", null); // false
Objects.equals("a", "a"); // trueNull in action
What does this print?
String a = null;
System.out.println(Objects.equals(a, null));
System.out.println("ok".equals(a));
System.out.println(a + "?");true false null?false false ?Throws NullPointerException
Show the answer
true, false, null?. Note the last one: string concatenation **turns null into the text "null"** instead of throwing.
isEmpty vs isBlank
isEmpty() is true only for length 0. isBlank() (Java 11) is also true for whitespace-only text like " ". Both are methods — so both throw on null. Check for null first.
"".isEmpty(); // true
" ".isEmpty(); // false (length 1)
" ".isBlank(); // trueOrder matters in ||
|| evaluates left to right and stops as soon as it finds true. Put the null check first, so the method call never runs on null.
// crashes: isEmpty runs on null
if (city.isEmpty() || city == null)
// safe: stops at city == null
if (city == null || city.isBlank())In real projects
Missing form fields, optional JSON properties and empty database columns all show up as null. That's why libraries like Apache Commons offer StringUtils.isBlank(s), which returns true for null too. The habit to build: null check first, then content check.
Key takeaways
- "literal".equals(x) is safe when x is null
- Objects.equals(a, b) is null-safe on both sides
- isEmpty: length 0; isBlank: only whitespace (Java 11)
- Check x == null first: || short-circuits
Tony Hoare, who introduced null references in ALGOL W in 1965, later called them his "billion-dollar mistake" because of the crashes they have caused ever since.
Practice questions
What does this print?
String name = null;
System.out.println("Bob".equals(name));
System.out.println(Objects.equals(name, null));- false true
- false false
- Throws NullPointerException
- true true
Check your answer
false true. "Bob".equals(null) returns false safely. Objects.equals returns true when both arguments are null.
What does this print?
System.out.println("".isEmpty());
System.out.println(" ".isEmpty());
System.out.println(" ".isBlank());- true true true
- true false true
- true false false
- false false true
Check your answer
true false true. isEmpty is true only when length is 0. A single space has length 1, but isBlank treats whitespace-only text as blank.