Backend Development 5 min read

Avoiding Excessive Null Checks in Java: When to Use Assertions, Exceptions, and the Null Object Pattern

The article explains why overusing null‑checks in Java leads to cluttered code, distinguishes between valid and invalid null returns, and recommends using assertions, explicit exceptions, empty collections, or the Null Object pattern to write cleaner, safer backend implementations.

Architecture Digest
Architecture Digest
Architecture Digest
Avoiding Excessive Null Checks in Java: When to Use Assertions, Exceptions, and the Null Object Pattern

In many Java projects developers frequently write if (someObject != null) { someObject.doCalc(); } to prevent NullPointerExceptions, which results in repetitive and ugly code. The article first clarifies that null can be either a valid response defined by the contract or an invalid, erroneous value.

Case 2 – Invalid null: When null represents an illegal argument (e.g., a missing required ID in an API), the method should abort and throw an exception. Two better alternatives to explicit null checks are suggested: using assert statements with descriptive messages, or directly throwing a NullPointerException.

Case 1 – Valid null: Sometimes null conveys the concept of “no result”, such as a database query that finds nothing. In this situation the article advises returning an empty collection instead of null, or returning a deliberately created empty object (a “null object”).

Example interfaces:

public interface Action { void doSomething(); }
public interface Parser { Action findAction(String userInput); }

Applying the Null Object pattern, the parser can return a constant DO_NOTHING action that safely does nothing:

public class MyParser implements Parser {
    private static final Action DO_NOTHING = new Action() {
        public void doSomething() { /* do nothing */ }
    };
    public Action findAction(String userInput) {
        // ...
        if (/* cannot find any action */) {
            return DO_NOTHING;
        }
    }
}

With this change, client code can call the action directly without any null checks:

ParserFactory.getParser().findAction(someInput).doSomething();

Additional practical tips include:

Prefer "bar".equals(foo) over foo.equals("bar") to avoid NPEs.

Use Optional (Java 8/Guava) to wrap potentially null values, reducing explicit checks.

When a method would otherwise return null, consider throwing an exception instead.

By distinguishing the two meanings of null and applying the recommended patterns, developers can eliminate redundant null‑checks and produce more concise, robust backend code.

backendJavabest practicesExceptionassertionnull-checkNull Object pattern
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.