views:

138

answers:

5

According to the documentation, List.contains can throw NullPointerException in this scenario:

"if the specified element is null and this list does not support null elements (optional)."

I was just trying to think of a List implementation that doesn't allow nulls though, and I'm not aware of any. For example, I can have ArrayList<Double>, but it allows nulls.

    List<Double> list = new ArrayList<Double>();
    if (list.contains(null)) { // this won't throw NPE            
    }

So is the documentation here referring to custom implementations of this interface, or are there some native JAVA collection classes that extend List that don't allow null elements? I realize the exception is optional, I was just trying to think of a real world case where this could occur.

+5  A: 

It's referring to custom implementations, until the day that one of the Java List implementations disallows null, and then it will be referring to that too.

Christoffer Hammarström
+1  A: 

--- Post Edited in response to comments ---

Basically the reason it's there is to remind you to catch the NullPointerExcepton because the designers of the List interface envisioned lists that might report any null access as an error.

--- Original Post Follows ---

The ones provided by the standard Java libraries support null, but there's no restriction on you creating a class that implements java.util.List which doesn't support nulls.

If the list doesn't support null, then checking for a null is equivalent to an error, so the exception might make sense depending on who implemented the List. That's why the interface has to mention the checked exception; because, if it didn't, then you couldn't throw a NullPointerException from the subclass should you have wanted to make sure that nobody every touched the list with a null.

Edwin Buck
Actually since NPE is a RuntimeException it's not necessary to declare it in the method signature (if it was, just about anything would have it, since it's such a common exception). More likely they're saying that they want to support lists that don't allow nulls, and in that case if you check if it contains a null value it will throw that exception intentionally, as opposed to all the runtime cases in which this might happen like trying to access properties of a null instance, when the VM throws it for you.
Andrei Fierbinteanu
It sounds like you're saying the same thing initially, but it seems that the main reason it's there is to remind you check for NPE because any List specific code is more likely to throw NPE (due to it's yet to be specified implementation).
Edwin Buck
Or possibly they're trying to cover for developers that might try to do `obj.equals` on the passed argument in their implementation of List without checking if it's null, and will end up giving a NPE without them realizing. :)
Andrei Fierbinteanu
True, but considering that such a "feature" would actually be considered a bug by most, I don't think they were trying to enable poor data sanitation on Collection inputs.
Edwin Buck
Plain and simple, the people who developed this API do not want you to catch an NPE. As Andrei said, they are covering for all developers. Josh Bloch (who architected the collections framework) abides by the Principle of Least Astonishment. The comment accounts for that.
John V.
+3  A: 

Not all implementations of List<...> allow for elements to be null.

An example is http://java.sun.com/j2se/1.5.0/docs/api/javax/management/relation/RoleList.html#add%28javax.management.relation.Role%29

This documentation prepares you for such an encounter, encouraging you to check the documentation of whatever list you're working with to see if it's a concern, or to err on the side of caution if you are unable to check it. Tracking down NPE's is not fun. Knowing documentation (provided good documentation exists) can save a lot of headaches.

glowcoder
Thanks for the answer and example. Thanks everyone else for their answers also, they were helpful, I just liked the way this answer succinctly explained things.
dcp
+2  A: 

Guava's ImmutableList disallows null, but it returns false on contains(null).

ColinD
Interesting. So does `ImmutableSet`. But maps created by `MapMaker` throw a `NullPointerException` on `containsKey(null)`
finnw
+1  A: 

A CheckedList would be an example of a List in the standard API that does not support nulls.

Yishai