Programs must not catch java.lang.NullPointerException
. A NullPointerException
exception thrown at runtime indicates the existence of an underlying null
pointer dereference that must be fixed in the application code (see rule EXP01-J. Never dereference null pointersDo not use a null in a case where an object is required for more information). Handling the underlying null pointer dereference by catching the NullPointerException
rather than fixing the underlying problem is inappropriate for several reasons. First, catching NullPointerException
adds significantly more performance overhead than simply adding the necessary null checks [Bloch 2008]. Second, when multiple expressions in a try
block are capable of throwing a NullPointerException
, it is difficult or impossible to determine which expression is responsible for the exception because the NullPointerException
catch
block handles any NullPointerException
thrown from any location in the try
block. Third, programs rarely remain in an expected and usable state after a NullPointerException
has been thrown. Attempts to continue execution after first catching and logging (or worse, suppressing) the exception rarely succeed.
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ERR08-J | medium | likely | medium | P12 | L1 |
...