Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5018849

(coll) TreeSet.contains(null) does not agree with Javadoc

XMLWordPrintable

      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)
      ======================================================================

            martin Martin Buchholz
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: