StackJay
StackJay

Reputation: 61

Shouldn't catch NPE but don't know how to handle the NULL

I know that NPE is not recommended to be caught, however the NPE is in a method whose return value is a boolean based on the variable where the NPE comes from.

Code is something like this:

public static boolean isStale() {
 return a.get();  //a==null causing NPE
}

In my case, I don't really know what to return without the info of a. What should I do? What kind of exception should I throw, if any?

Edit:
1. returning true or false will be inaccurate because the result really depends on the info inside a.

  1. a is acctually from a service call client.getA()

Upvotes: 0

Views: 228

Answers (4)

sstan
sstan

Reputation: 36483

First you need to ask yourself: Is it normal for a to ever be null?

If yes, then throwing an exception is rarely a good idea. Exceptions should really only be thrown when something unexpected occurs. It then really is up to you to decide what this means for your program. Does it make sense to return some default value in this case?

return a == null ? false : a.get(); // return false as a default value?

If no (more likely):

... then ask the following question: Why am I getting a null value?

If it's because you coded a bug: then you don't need to do anything. Just let it throw a NullPointerException. There really is no point in catching the exception and pretending everything is alright, if clearly, a basic invariant in your program has been broken. Better to let it throw the exception early and focus your energies on understanding and fixing the bug asap. Trying to handle the exception will only cause your program to fail at some other more unexpected point in time, and will become that much harder to debug.

If, on the other hand, it's because of some external entity feeding you bad data over which you really have no control: then this could be a good case for throwing a custom exception that can be used to provide a more meaningful error message to either the user or the developer. But even in this case, it's probably a better idea to have a validated for a null value more "upstream" and have it throw an exception as early as possible. It doesn't feel like it should be part of the isStale() method's contract to worry about the correctness of a's value.


Interesting reading, geared towards C#, but the principles apply just as well to Java: Vexing exceptions.

Upvotes: 1

Dici
Dici

Reputation: 25950

There is no reason why you should prevent your code from throwing exceptions. Just consider your case : you say yourself that the behaviour of your method is not defined in the case a == null, so why should you handle it ? It will only lead to weird behaviours and your users might be surprised that their code failed or produce unexpected results somewhere whereas their calls into your API went fine.

Exceptions are specially designed to address this kind of things, so make good use of it. You can either throw a new, custom exception to add some API-specific semantic, or let the NPE be propagated to the caller.

For example :

public static boolean isStale() {
     if (a == null) throw new IllegalStateException("a should not be null");
     return a.get();
}

However, it is more likely that you should do that in a constructor (because I assume a is an attribute). In a constructor, I would opt for IllegalArgumentException.

Upvotes: 5

cнŝdk
cнŝdk

Reputation: 32145

There's no need to throw any Exception just make sure a isn't null before using it, use a simple conditional statement:

public static boolean isStale() {
  a!=null ? return a.get() : return false;  
}

In the case where returning false will be irrelevant, you can just change the return type of your method to String and return a warning or any other significant message :

public static String isStale() {
  a!=null ? return a.get().toString() : return "NULL";  
}

Take a look at this: Best approach to converting Boolean object to string in java

Upvotes: 0

Bill Rose
Bill Rose

Reputation: 111

This depends on what isStale() determines. You can try/catch this method and catch the NullPointerException. I don't believe this violates best practices if you really have no way to handle a being null. You could also instantiate a new a if a is null, or throw your own Exception.

Upvotes: 0

Related Questions