Invalid NullPointerException in set

From Java docs removeAll() to Set:

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

If we look at the above statement, the following code should not throw a null pointer exception; however, when I run this program, I get it.

 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 someone tell me why I am getting an exception here?

+5
source share

Source: https://habr.com/ru/post/1269958/


All Articles