The OpenJDK implementation of AbstractSet throws a NullPointerException when you pass a null value to removeAll.
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/util/AbstractSet.java
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
e.g.
Set<String> parent = new HashSet<String>();
Set<String> children = null;
parent.removeAll(children);
Results in
Exception in thread "main" java.lang.NullPointerException
at java.util.AbstractSet.removeAll(AbstractSet.java:152)
What is the architectural logic behind this?
Why wouldn't it do a null check and return modified = false
instead of throwing a NullPointerException?
What is the benefit in this case between differentiating between null and an empty set?
EDIT:
I know the interface specifies it. You can ask the same question about the interface. The point of the question is that removing nothing from something leaves you with the original. There doesn't seem to be a logical reason for throwing a NullPointerException.
For those that say any null should be fixed, that isn't a logical reason for throwing a NullPointerException in this case.
It isn't a duplicate question of What is a NullPointerException, and how do I fix it? because I am asking about the architectural design of a specific method and not asking what a NPE is.