5

From the Java docs of removeAll() in Set:

throws NullPointerException - if this set contains a null element and the specified collection does not permit null elements

If we go by the above statement, the following code should not throw a null pointer exception; however, when I run this program, I am getting one.

public class Test {
    public static void main(String[] args) {
        Set<String> s = new TreeSet<String>();
        s.add("A");
        s.add("B");
        Set<?> s1 = new HashSet<Object>();
        s1.add(null);
        s.removeAll(s1);
    }
}

Can some one tell me why I am getting an exception here?

Ry-
  • 218,210
  • 55
  • 464
  • 476
T-Bag
  • 10,916
  • 3
  • 54
  • 118

0 Answers0