frjaeger220 / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

NullPointerException in Guice.createInjector(...) with null module #260

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
This is a bit trivial, but:

Module mod = null;
Guice.createInjector(Stage.Development, mod)

results in:

java.lang.NullPointerException
  at com.google.inject.BinderImpl.install(BinderImpl.java:199)
  at com.google.inject.Guice.createInjector(Guice.java:77)
  at com.google.inject.Guice.createInjector(Guice.java:64)

Although NullPointerException is possibly a legitimate error to throw here,
IllegalArgumentException would be preferable due to the frequency with
which NPEs are associated with programming errors.

Original issue reported on code.google.com by kris.nuttycombe on 14 Oct 2008 at 11:56

GoogleCodeExporter commented 9 years ago
Passing null to Guice.createInjector is a programming error. NPE is the right 
exception.

Original comment by limpbizkit on 15 Oct 2008 at 1:31

GoogleCodeExporter commented 9 years ago
The primary issue I have with NPE as apposed to IllegalArgumentException is that
though the null dereference is an error, the fact that it's a few layers deep 
in the
stack trace violates the "fail fast" principle. IllegalArgumentException is also
indicative of a programming error, and more informative.

Original comment by kris.nuttycombe on 15 Oct 2008 at 3:59

GoogleCodeExporter commented 9 years ago
I note also that the Javadoc makes no reference to the fact that null arguments 
are
not valid.

Original comment by kris.nuttycombe on 15 Oct 2008 at 4:04

GoogleCodeExporter commented 9 years ago
FYI, all of the methods in Guice throw NullPointerException given null 
arguments unless null is explicitly 
permitted. 

There have been many IllegalStateException vs. NullPointerException flamewars. 
We've decided NPE is the best 
choice, if only because it's a lot easier to enforce - just dereference the 
parameter.

In any case, the distinction is insignificant. Both exceptions indicate a 
programming error, and it is a  a mistake 
to catch either exception explicitly. (Catching RuntimeException is okay for 
fault-tolerance)

Original comment by limpbizkit on 15 Oct 2008 at 5:52