Throwing an exception that's exclusive to null
arguments (whether NullPointerException
or a custom type) makes automated null
testing more reliable. This automated testing can be done with reflection and a set of default values, as in Guava's NullPointerTester. For example, NullPointerTester
would attempt to call the following method...
Foo(String string, List> list) {
checkArgument(string.length() > 0);
// missing null check for list!
this.string = string;
this.list = list;
}
...with two lists of arguments: "", null
and null, ImmutableList.of()
. It would test that each of these calls throws the expected NullPointerException
. For this implementation, passing a null
list does not produce NullPointerException
. It does, however, happen to produce an IllegalArgumentException
because NullPointerTester
happens to use a default string of ""
. If NullPointerTester
expects only NullPointerException
for null
values, it catches the bug. If it expects IllegalArgumentException
, it misses it.