Master Elegant Null Checks in Java with StringUtils and ObjectUtils

This article explains how to replace repetitive !=null checks in Java with concise, type‑aware utility methods such as StringUtils, ObjectUtils, Collections, and CollectionUtils, providing code examples and discussing the underlying implementations and their limitations for different data types.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
Master Elegant Null Checks in Java with StringUtils and ObjectUtils

NullPointerException is a common bug in Java, and developers often solve it by adding a != null check. When such checks appear frequently, a more efficient and elegant approach is needed.

Step 1

Before performing a != null check, identify the data type of the variable (e.g., String, custom Object, List, array, Map).

Step 2

Choose the appropriate utility class for the identified type:

String : StringUtils Object : ObjectUtils Array : Arrays List and Map : Collections, CollectionUtils,

ObjectUtils

Step 3

Use the selected utility class to perform the null/empty check.

Examples:

String str = "";
StringUtils.isEmpty(str); // true
public static boolean isEmpty(@Nullable Object str) {
    return str == null || "".equals(str);
}
Object obj = null;
ObjectUtils.isEmpty(obj); // true
Map<String, Object> map = Collections.emptyMap();
ObjectUtils.isEmpty(map); // true
List<Integer> list = Collections.EMPTY_LIST;
ObjectUtils.isEmpty(list); // true
Object[] objArr = null;
ObjectUtils.isEmpty(objArr); // true

The ObjectUtils.isEmpty(Object obj) method handles various types (Optional, CharSequence, arrays, collections, maps) and returns true if the object is null or considered empty.

public static boolean isEmpty(@Nullable Object obj) {
    if (obj == null) {
        return true;
    }
    if (obj instanceof Optional) {
        return !((Optional) obj).isPresent();
    }
    if (obj instanceof CharSequence) {
        return ((CharSequence) obj).length() == 0;
    }
    if (obj.getClass().isArray()) {
        return Array.getLength(obj) == 0;
    }
    if (obj instanceof Collection) {
        return ((Collection) obj).isEmpty();
    }
    if (obj instanceof Map) {
        return ((Map) obj).isEmpty();
    }
    return false;
}

However, this method only checks collection size, not whether each element is null. For a list containing a single null element, ObjectUtils.isEmpty(list) returns false.

List<Integer> list = Collections.singletonList(null);
ObjectUtils.isEmpty(list); // false

To verify that every element in a list or array is null, iterate over the elements, e.g.:

Arrays.stream(list.toArray()).allMatch(ObjectUtils::isEmpty);

For maps, CollectionUtils.isEmpty(map) aggregates a null check and isEmpty() call:

public static boolean isEmpty(@Nullable Map<?, ?> map) {
    return map == null || map.isEmpty();
}

Similarly, CollectionUtils.isEmpty(Collection<?> collection) checks both null and emptiness:

public static boolean isEmpty(@Nullable Collection<?> collection) {
    return collection == null || collection.isEmpty();
}

Conclusion

Determining whether a value is null can be streamlined in three steps: identify its type, select the appropriate utility class, and apply the class’s null/empty check.

Use StringUtils for String values.

Use ObjectUtils for all other types.

For List and Map, CollectionUtils offers additional convenience methods.

When checking that every element in a collection or array is null, iterate over the elements.

These practices help write cleaner, more maintainable Java code.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavanullpointerexceptionBestPracticesUtilityClassesStringUtilsObjectUtils
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

0 followers
Reader feedback

How this landed with the community

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.