JDK-5018849 : (coll) TreeSet.contains(null) does not agree with Javadoc
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2004-03-23
  • Updated: 2012-10-08
  • Resolved: 2005-09-04
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b51Fixed
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 We are adding many missing specifications for NullPointerException and ClassCastException throughout the Collections hierarchy.
22-08-2005

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
24-03-2004