Comparable
contract specifies that e.compareTo(null)
must throw NullPointerException
.
From the API:
Not
Is it ever a good idea to even have to sort a List containing null elements, or is that a sure sign of a design error?
Conceptually, null means "nothing", and placing nothing in a list seems weird to me. Also, the Java List contract states that
Some list implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements
so a List implementation in Java is not even required to support null elements at all. To sum up, if you do not have a good reason to put null into a list, don't, and if you do, test that it actually works as expected.