How to notify the programmer of a null argument?

So, I am working on creating a class in which if certain arguments for certain methods are equal to zero, either the method (or the object as a whole) will not work.

I know that he will throw a NullPointerException after receiving a null object and try to use it, but I want the programmer to try to call the method to understand that the error is not in my code. I just want to make sure that the resulting exception would be very clear (without having to look into my source).

I saw some examples of what I described where they throw an IllegalArgumentException when the parameter is null.

Here's the difference, imagine that someObject will somehow be vital to the method:

 public void doSomething(SomeClass someObject) { if (someObject == null) throw new IllegalArgumentException("someObject is null"); ... } 

Thus, the programmer understands that he violated the contract implied by javadoc (regardless of whether this is explicitly indicated).

Is this a good practice or even a sensible thing?


Quick Editing / Sidebar:

What is better to say in the exception message?

Better to say that โ€œwent wrongโ€:

someObject is null

Or is it better to say that something โ€œwent wrongโ€ and generally implies a reason (and ultimately a solution):

someObject cannot be null

Or is there a better alternative?

+4
source share
5 answers

Use IllegalArgumentException instead of resolving NullPointerException so that you can detect the error as early as possible. You can do the same check and throw a NullPointerException yourself, but this is a matter of semantics.

Throwing an error immediately will help you or another developer catch the error before anything else happens. If your method does not use the argument immediately (for example, in the setter), then the problem may seem to be the result of some completely different operation.

If you are not familiar with this, a fantastic book called The Pragmatic Programmer has a hint that I am trying to emphasize here:

Time 32: Early Early

The basic idea is to fail before something bad happens, and not solve problems in unexpected moments.

It would also be a good place to use the statement to enforce your precondition. Make sure you confirm that the argument cannot be null and make your method like this:

 public void doSomething(SomeClass someObject) { assert someObject != null ... } 

If the statement fails, an error will be thrown. Although this can be disabled using compiler options, this is good practice during development.

+5
source

I would say that standard documented practice is just to use a NullPointerException .

  • It is used explicitly in many @throws Javadocs to document the case when the argument is null and not resolved ( Reader.read(CharBuffer) , String.contains(CharSequence) and many, many others)
  • Recommended according to coding guidelines (e.g. Bloch recommends using it in Effective Java).
  • The class itself says that it should be explicitly cast:

    Applications should throw instances of this class to indicate other illegal actions of the null object.

  • This is what Guava Preconditions throws for checkNotNull , and they know the design of the Java library.
  • Using NullPointerException does not stop you from giving a good error message describing the exception. By doing so, you set up such stacks besides the standard NPE stacktrace.
If you really want to deviate from the standard, you can use IllegalArgumentException , but at that moment I would go further and create a subclass called NullArgumentException .

Edit

Speaking of preconditions, you seem to be trying to use runtime exceptions to check values โ€‹โ€‹and invariants. I highly recommend that you use the Guava Preconditions library to standardize and simplify this. Your example simply boils down to the following:

 public void doSomething(SomeClass someObject) { Preconditions.checkNotNull(someObject, "someObject cannot be null"); //... } 
+4
source

You can also use. Just write down your choice and be consistent. If customers are more likely to use your library with another, then this is consistent with what is likely to also help customers.

+1
source

Both approaches are reasonable. If your javadoc says that passing null will lead to NPE and the user of your API will get NPE, then that sounds to me how the function works as advertised!

I find that NPE is easiest when an argument should be used in this method. If you are going to save the link later (for example, in the constructor), then an IllegalArgumentException will make more sense. In other words, throw away the NPE if this happens naturally, and throw the IAE if it saves the future NPE.

+1
source

I prefer to use NPE, but I would confirm the input at the beginning of the method, because otherwise my object state could be changed to something illegal (due to some incomplete work).

0
source

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


All Articles