Is the use of NullPointerException recommended in this case:
/**
* Drop the database referenced by the singleton.
*
* @throws NullPointerException
*/
public static void dropDatabase() throws NullPointerException {
if (store != null) {
store.dropDatabase(DATABASE);
} else {
throw new NullPointerException("No valid database connection.");
}
}
Well it’s pretty pointless code in that you’d still get a
NullPointerExceptionif it unconditionally calledstore.dropDatabase. You wouldn’t get the same message, but the stack trace would make it pretty clear.In this case I would say it should be an
IllegalStateExceptionthough:Sounds exactly like the situation to me. I’d also do the check first, like this:
That way you can get all the preconditions out of the way at the start of the method, and then focus on the main body.
With Guava, I’d just change this to: