Java Null Checks, List and String Validation, and Optional Usage Guide
This article explains common Java null‑checking techniques for objects, Lists, and Strings, compares different utility methods, and introduces the Optional class with its creation patterns and typical usage scenarios to help avoid NullPointerException in backend development.
1. Preface
In real projects many places require null‑checking; without it a NullPointerException may be thrown.
We previously mentioned exception handling; now we look at common null‑checking methods.
Typical ways to check an object for null include java.util.Objects.nonNull(obj), Hutool's ObjectUtil, or simply null != obj .
2. List Null‑Check
For a List, being non‑null is different from having a size greater than zero; a null list means the container does not exist, while size 0 means it is empty.
Some developers use list.isEmpty() , which internally throws NullPointerException if the list is null.
Therefore the common pattern is list != null && list.size() > 0 or using Hutool's CollUtil.isEmpty. The same applies to Set and Map.
3. String Null‑Check
Calling methods like equals(...) or length() on a null String also throws NullPointerException.
Common approaches:
if (a == null || a.equals("")) { /* ... */ }or more efficient:
if (a == null || a.length() == 0) { /* ... */ }Since Java 6 you can use a == null || a.isEmpty() , or Apache Commons StringUtils.isNotBlank(a) / StringUtils.isNotEmpty(a) with their respective semantics.
4. Optional
Java's Optional class is designed to avoid NullPointerException. Common factory methods include .empty() , .of(T) , .ofNullable(T) . Useful instance methods are isPresent() , ifPresent(...) , orElse(...) , orElseGet(...) , orElseThrow() , map(...) , flatMap(...) , and get() .
Example code showing Optional creation:
public final class Optional
{
private static final Optional
EMPTY = new Optional<>();
private final T value;
private Optional() { this.value = null; }
private Optional(T value) { this.value = Objects.requireNonNull(value); }
public static
Optional
empty() {
@SuppressWarnings("unchecked")
Optional
t = (Optional
) EMPTY;
return t;
}
public static
Optional
of(T value) { return new Optional<>(value); }
public static
Optional
ofNullable(T value) { return value == null ? empty() : of(value); }
}4.1 Optional Object Creation
See the code block above for the internal implementation of the Optional class.
4.2 Usage Scenarios
Scenario 1: In a service layer, query an object, then use Optional to handle possible null values.
Scenario 2: Combine Optional with functional programming to achieve the same logic in a single line.
5. Summary
Each method has its appropriate scenario; while fluent APIs like Optional can make code more elegant, they may reduce readability, so choose according to project needs.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.