Never return Null Arrays!

Continuing on Jeanne’s theme of nulls, its a pet peeve of mine when I come across code that returns null arrays instead of empty arrays. The purpose of this post is to discuss some of the reasons why its a good practice to return empty arrays over null arrays, including Collection objects or typed array.

Null Pointer Exception

Consider the reusability of the following code:

public List getItems() {
   ...
   // There are no items, return null
   return null;
}

Let’s say you want to iterate on the results, the following code would throw a NullPointerException if used in conjunction with the code above:

List items = getItems();
for(int i=0; i<items.size(); i++) {
   // If there are no items, this code will throw a null pointer
}

Iterating on an array is one of the most commons practices in Java, so returning null instead of an empty array means the person is going to have to do extra null checks when they really don’t need to. In this situation, an empty array would suffice and would not produce any errors.

To summarize, it makes the code shorter, easier to read, and less likely to throw a NullPointerException. Also, there is some confusion with returning null since you may (or may not) be saying an empty array is the same thing as null. For example, the following would be confusing logic:

List items = getItems();
if(items == null) {
   // If null do one thing
} else if(items.size==0) {
   // If empty do another?
}

In this situation its not clear why you might act differently on the two return values and this can lead to confusing, ambiguous code.

The only time I might consider returning null valid is if there was an error of some kind, but then you should be throwing an exception (something more programmers are hesitant to do). Think of it like this, would you rather read a stack trace with a detailed error what went wrong in the code created by you, or would you rather see NullPointerException and wonder what of a dozen objects might have been null?

7 thoughts on “Never return Null Arrays!

  1. And remember that you can easily return an empy Collection (List, Set, …) with
    java.util.Collections.EmptyList (EmptySet, EmptyMap). You don’t have to allocate a new object.

  2. Good advice, seldom followed. We should strive to avoid “if”, as a successor to Dijkstra, “if is evil”. Remember, Collections define EMPTY_LIST, EMPTY_SET and EMPTY_MAP which are very handy in these cases (and reads better than alternatives).

  3. Completely agree, never return null instead of an empty collection. In general, require that the client performs null checks on the return value of a function is also a bad idea, usually there are better ways to indicate errors.
    The approach you suggest is also recommended in Effective Java by J. Bloch.

  4. Someone reading this might be confused about what the difference between Lists and arrays might be. I suggest that your examples should match your text.
    Perhaps you could comment that it a good idea not to return null collections or arrays.

  5. Hi Peter,
    It’s a much larger issue to distinguish between java primitive arrays and Java Collection classes, but my advice applies to both WLOG.
    Thanks,
    -Scott

Leave a Reply

Your email address will not be published.