JDK-4804498 : Nulls can't be removed from TreeSets
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 1.4.0,1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: solaris_7,windows_2000
  • CPU: x86,sparc
  • Submitted: 2003-01-16
  • Updated: 2021-03-03
  • Resolved: 2003-01-22
Related Reports
Relates :  
Description

Name: rmT116609			Date: 01/16/2003


FULL PRODUCT VERSION :
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)


FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]
(C) Copyright 1985-2000 Microsoft Corp.

A DESCRIPTION OF THE PROBLEM :
Nulls cannot be removed from TreeSet

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Add a null to a TreeSet.
2. Try to remove it.
3. Profit!!!!

EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected:
class java.util.HashSet
[null]
[]
class java.util.TreeSet
[null]
[]

Actual:
class java.util.HashSet
[null]
[]
class java.util.TreeSet
[null]
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.compare(TreeMap.java:1081)
        at java.util.TreeMap.getEntry(TreeMap.java:341)
        at java.util.TreeMap.remove(TreeMap.java:500)
        at java.util.TreeSet.remove(TreeSet.java:218)
        at Test.main(Test.java:17)

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.compare(TreeMap.java:1081)
        at java.util.TreeMap.getEntry(TreeMap.java:341)
        at java.util.TreeMap.remove(TreeMap.java:500)
        at java.util.TreeSet.remove(TreeSet.java:218)
        at Test.main(Test.java:17)

REPRODUCIBILITY :
This bug can be reproduced always.

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

public class Test {

    public static void main(String args[]) {
        Set set = new HashSet();
        System.out.println(set.getClass());
        set.add(null);
        System.out.println(set);
        set.remove(null);
        System.out.println(set);

        set = new TreeSet();
        System.out.println(set.getClass());
        set.add(null);
        System.out.println(set);
        set.remove(null);
        System.out.println(set);
    }

}
---------- END SOURCE ----------

CUSTOMER WORKAROUND :
set = new HashSet(set);
set.remove(null);
set = new TreeSet();
(Review ID: 164182) 
======================================================================

Comments
EVALUATION It is possible to insert and remove null elements from TreeSets, however you must provide an appropriate (null-aware) comparator. If you do not provide a Comparator at create time, the set is sorted according to the elements' natural ordering, which does *not* include null. It is somewhat unfortunate that TreeSet allows you to add a null element to an empty set with no Comparator, given that you can't then remove it. On the other hand, it is not clear whether it's worth special-casing this; it would slow down all TreeSets (and TreeMaps) with no real benefit. Generally speaking, you will get ClassCastExceptions when you put elements into sorted set that are not mutually comparable or are not comparable using the set's comparator. The NullPointerException in this case plays the part of the ClassCastException, but it should really be thrown when the null is inserted, not when it is removed. ###@###.### 2003-01-22
22-01-2003