United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-5018849 (coll) TreeSet.contains(null) does not agree with Javadoc
JDK-5018849 : (coll) TreeSet.contains(null) does not agree with Javadoc

Details
Type:
Bug
Submit Date:
2004-03-23
Status:
Resolved
Updated Date:
2012-10-08
Project Name:
JDK
Resolved Date:
2005-09-04
Component:
core-libs
OS:
linux
Sub-Component:
java.util:collections
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.4.2
Fixed Versions:

Related Reports

Sub Tasks

Description
Name: jl125535			Date: 03/23/2004


FULL PRODUCT VERSION :
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_02-b03)
Java HotSpot(TM) Client VM (build 1.4.2_02-b03, mixed mode)

java version "1.5.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b43)
Java HotSpot(TM) Client VM (build 1.5.0-beta2-b43, mixed mode)

A DESCRIPTION OF THE PROBLEM :
The Javadoc for TreeSet.contains() at http://java.sun.com/j2se/1.5.0/docs/api/java/util/TreeSet.html#contains(java.lang.Object) does not specify that it will throw a NullPointerException under any circumstances.

When TreeSet.contains() is called with a null parameter and the set is empty, false is returned.

When TreeSet.contains() is called with a null parameter and the set is not empty, a NullPointerException is thrown.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1) instantiate a TreeSet
2) add an element to the TreeSet
3) call TreeSet.contains(null)

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
false should be returned if the set does not contain null, true if it does
ACTUAL -
NullPointerException thrown

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.compare(TreeMap.java:1085)
        at java.util.TreeMap.getEntry(TreeMap.java:345)
        at java.util.TreeMap.containsKey(TreeMap.java:203)
        at java.util.TreeSet.contains(TreeSet.java:195)
        at Test.main(Test.java:10)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.TreeSet;

public class Test
{
    public static void main(String[] args)
    {
        TreeSet s = new TreeSet();
        System.out.println(s.contains(null));
        s.add(new Object());
        System.out.println(s.contains(null));
    }
}

---------- END SOURCE ----------
(Incident Review ID: 244101) 
======================================================================

                                    

Comments
EVALUATION

This is a grey area of the spec.  If you look at Set.contains(Object), you'll see that it says:

Throws:

   NullPointerException - if the specified element is null and the set does not support null elements (optional).

Arguably, the word "optional" legitimizes the current behavior.  It's not clear that it is worth changing the behavior.

###@###.### 2004-03-24

Will be addressed as part of JSR166 maintenance.
###@###.### 2005-03-09 03:08:35 GMT
                                     
2004-03-24
EVALUATION

We are adding many missing specifications for
NullPointerException and ClassCastException throughout the Collections hierarchy.
                                     
2005-08-22



Hardware and Software, Engineered to Work Together