Explain Codes LogoExplain Codes Logo

Does java.util.List.isEmpty() check if the list itself is null?

java
defensive-programming
utility-functions
null-checks
Anton ShumikhinbyAnton Shumikhin·Feb 5, 2025
TLDR

No, List.isEmpty() does not check if the list itself is null. It throws a NullPointerException if you call it on a null object. Here is how to prevent this:

if (list == null || list.isEmpty()) { // Handle null or empty list like a pro }

This code snippet performs null-safe check for an empty list. TOMT (Tip of My Tongue), remember this!

Prequel to null-ception

Before you start throwing methods at objects like there's no tomorrow, consider taking a moment to check if that object even exists. Watch out for an unexpected NullPointerException:

if(list != null) { // I see null people! boolean isEmpty = list.isEmpty(); }

This defensive programming prevents the dreaded NullPointerException from crashing the party.

Null isn't always scary

Sure, you don't want your code to be filled with mysterious null checks every line, but have no fear, libraries are here!

  1. Apache Commons Collections: Sports a nifty function CollectionUtils.isEmpty() that checks if a list is null or empty.
  2. Spring Framework: Aren't all seasons great when CollectionUtils.isEmpty() performs a null-safe check on your collection.

Here's how to use these utility functions:

boolean isSafeEmpty = CollectionUtils.isEmpty(list);

Why DIY, when you can automate!

If adding null checks throughout your code seems redundant, why not create your own function to cover both checks?

public static boolean isNullOrEmpty(List<?> list) { return list == null || list.isEmpty(); }

A custom utility function can keep your code DRY (Don't Repeat Yourself) and highlight your pro coding skills like:

if (isNullOrEmpty(list)) { // Null or empty, I've got it handled! }

Avoid issues before they occur

An ounce of prevention's worth a pound of cure, or in developer lingo, initializing your lists could save you a day of debugging:

List<String> myList = new ArrayList<>(); // Initialize like nobody's watching

Being consistent through thick and thin

For consistency, apply a single approach across your codebase to handle potential nulls:

  1. Document: Outline your modus operandi about null-check strategies.
  2. Scan: Regularly review code to avoid NullPointerExceptions.
  3. Test: Include unit tests for your list references and edge cases.

"Being empty is so rude!" said the Null

In the kingdom of programming, there's a profound difference between a null list and an empty list. Visualize it this way:

  • "I bought a new basket" (💡 - basket is List object) are there any apples in it? 🍎
  • "No, not yet" (🔋 - isEmpty checks presence of apples in the basket)
List<String> basket = new ArrayList<>(); if(basket.isEmpty()) { // Look ma, no apples! }

isEmpty checks for apples (List elements), but NOT the existence of basket (List object).

Adopting a consistent approach to managing potential nulls leads to stronger, leaner code. Whether you choose null checks, utility methods, or instantiating lists right at declaration, consistency is key.

  1. Comments: Document your choice of null-check strategies for better team alignment.
  2. Code reviews: Regularly review code for potential null ref slip-ups.
  3. Testing: Include thorough unit tests to cover null and edge cases