1

It's confusing: We all know the words from Doug Lea: "Null sucks". He is right.

Now I there are some approaches to throw exceptions by using googles guava. But what would be the right way now?

1) throwing an NullPointerException

private void foo(Object arg) {
  Preconditions.checkNotNull(arg, "arg 'null' not accepted");
}

2) or an IllegalArgumentException

private void foo(Object arg) {
  Preconditions.checkArgument(arg!=null, "arg 'null' not accepted");
}

3) or a VerifyException

private void foo(Object arg) {
  Verify.verify(arg!=null, "arg 'null' not accepted");
}

I mean, isn't the first approach obsolete cause we don't have a NullPointer access at this time? And why this new third approach.

Could someone lift the fog?

John Rumpel
  • 4,535
  • 5
  • 34
  • 48
  • Depending on the analysis, from time to time, null is a valid response. If it is not, try and figure out whether it was an error in the business logic, or the technical background that caused the null. – Stultuske Feb 18 '15 at 08:59
  • 1
    Update to Java8 and return `Optional` – Konstantin Yovkov Feb 18 '15 at 09:00
  • Check out the [docs](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Verify.html#verifyNotNull(T)) for guidance on the same. They can be used interchangeably for most situations. – Deepak Bala Feb 18 '15 at 09:08
  • I agree that Optional is good approach, but sometimes you need to throw excpetion (Transaction rollback, etc.). As you checking argument, I will go with IllegalArgumentException. You can have many cases when argument is illegal, NULL is one of them. (i.e. NULL, below zero, is even, etc. ) – Lukino Feb 18 '15 at 09:22
  • 2
    possible duplicate of [IllegalArgumentException or NullPointerException for a null parameter?](http://stackoverflow.com/questions/3881/illegalargumentexception-or-nullpointerexception-for-a-null-parameter) – Olivier Grégoire Feb 18 '15 at 09:28
  • The built in Java libraries come down pretty firmly on the side of throwing NPE. – Louis Wasserman Feb 18 '15 at 16:57
  • Thanks a lot. Optional is also part of guava by the way. And this is a proper way to signal that 'null' is a valid input (or return value). I'm going to use it in the future and skip preconditions assuming that 'null' is never a valid input and should be checked by the calling function. – John Rumpel Feb 19 '15 at 18:20

3 Answers3

4

I agree with Aaron. My two cents is that since in Java 7 Objects.requireNonNull was added and it throws a NullPointerException, that is the way I go. To me it seems like the language developers have weighed in on this issue by doing so.

John B
  • 32,493
  • 6
  • 77
  • 98
3

There is no right way. Some people argue NPE since you passed a null pointer. Plus you can directly use Preconditions.checkNotNull() without any additional checks in code that you have to write (!=null is another 6-10 key presses, after all, plus people have to read more code to understand what you're doing).

Preconditions.checkArgument() makes more sense from an API point of view since the method doesn't want null arguments. Plus you have additional code to write and to read when you're debugging.

Verifify seems too weak for this for me, after reading the documentation.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
1

"Effective Java" seems quite adamantly in favor of throwing a NullPointerException IllegalArgumentException or NullPointerException for a null parameter?

Community
  • 1
  • 1
Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60