3

Is it better to wait for a null pointer exception to happen?

public void doSomething(String str) {

    Double val = Double.parseDouble(str); // Null pointer exception thrown here


    // Other code
}

Or is it better to check every time for it, as early as possible?

public void doSomething(String str) {

    if (str == null)
        throw new NullPointerException(); // Null pointer exception thrown here

    Double val = Double.parseDouble(str);           // Other code
}
ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
user1883212
  • 7,539
  • 11
  • 46
  • 82

5 Answers5

1

I would recommend using an assert clause. I think his response best answers your question

Avoiding != null statements

Community
  • 1
  • 1
af3ld
  • 782
  • 8
  • 30
1

I would indicate in your method if str can be null or not with the @Nullable keyword. If you are disallowing the str variable to be null then do not do any null checking. You should instead check if str is null before calling doSomething. If str is allowed to be null then wrap it in a null check and do whatever you deem to be appropriate if the variable is null.

public void doSomething(@Nullable String str) {

    if (str != null) {
        Double val = Double.parseDouble(str);

        // other code
    }
    else {
        // return or do something else
    }
}

Or..

public void doSomething(@Nullable String str) {

    if (str == null) {
        return;
    }

    Double val = Double.parseDouble(str);

    // other code

}

I would not recommend throwing a null pointer error unless the application cannot continue without the str variable. You want to capture exceptions so your application doesn't crash not allow them to crash your application.

vguzzi
  • 2,420
  • 2
  • 15
  • 19
1

In that case it doesn't make much difference as parseDouble will throw a NPE. In a more general case, since Java 7, you can use:

Objects.requireNonNull(str); //throws NPE if str is null
//rest of the code
assylias
  • 321,522
  • 82
  • 660
  • 783
0

There no different code. You should throw some specific exception. And sometimes null is legal value.

VDanyliuk
  • 1,049
  • 8
  • 23
0

Sometimes it is usual to define something like a method contract.

I am doing this via Spring Asserts:

org.springframework.util.Assert;

Assertion utility class that assists in validating arguments. Useful for identifying programmer errors early and clearly at runtime.

For example, if the contract of a public method states it does not allow null arguments, Assert can be used to validate that contract. Doing this clearly indicates a contract violation when it occurs and protects the class's invariants.

Typically used to validate method arguments rather than configuration properties, to check for cases that are usually programmer errors rather than configuration errors. In contrast to config initialization code, there is usally no point in falling back to defaults in such methods.

This class is similar to JUnit's assertion library. If an argument value is deemed invalid, an IllegalArgumentException is thrown (typically).

In your case:

public void doSomething(String str) {
   Assert.notNull(str);

   Double val = Double.parseDouble(str); // Nullpointer not possible here if the contract was not injured.


        // Other code
}

If a null value is passed by any developer the contract was not fullfilled and a IlligalArgumentException is thrown.

Easy testable via Junit:

  /**
   * Check case that passed string is null.
   */
  @Test(expected = IllegalArgumentException.class)
  public void testDoSomething_StringIsNull() {
    mClassUnderTest.doSomething(null);
  }
nano_nano
  • 12,351
  • 8
  • 55
  • 83