Do Not Catch NullPointerException
ID |
java.no_catch_npe |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
CWE:395, reliability |
Rationale
NullPointerException is a programming error that indicates a null dereference. Catching it masks the root cause instead of fixing it. The correct approach is to add null checks or use Optional before the point where the NPE would occur.
// Bad — masks the null dereference bug
try {
result = obj.getValue().length();
} catch (NullPointerException e) {
result = 0;
}