Use of NullPointerException Catch to Detect NULL Pointer Dereference |
Weakness ID: 395 (Weakness Base) | Status: Draft |
Description Summary
Catching NullPointerException should not be used as an alternative to programmatic checks to prevent dereferencing a null pointer.
Example 1
The following code mistakenly catches a NullPointerException.
(Bad Code)
Example Language: Java
try {
mysteryMethod();
} catch (NullPointerException npe) {
}
Do not extensively rely on catching exceptions (especially for validating user input) to handle errors. Handling exceptions can decrease the performance of an application. |
Programmers typically catch NullPointerException under three circumstances: 1. The program contains a null pointer dereference. Catching the resulting exception was easier than fixing the underlying problem. 2. The program explicitly throws a NullPointerException to signal an error condition. 3. The code is part of a test harness that supplies unexpected input to the classes under test. Of these three circumstances, only the last is acceptable. |
Nature | Type | ID | Name | View(s) this relationship pertains to![]() |
---|---|---|---|---|
ChildOf | ![]() | 388 | Error Handling | Seven Pernicious Kingdoms (primary)700 |
ChildOf | ![]() | 389 | Error Conditions, Return Values, Status Codes | Development Concepts (primary)699 |
ChildOf | ![]() | 691 | Insufficient Control Flow Management | Research Concepts (primary)1000 |
ChildOf | ![]() | 755 | Improper Handling of Exceptional Conditions | Research Concepts1000 |
Mapped Taxonomy Name | Node ID | Fit | Mapped Node Name |
---|---|---|---|
7 Pernicious Kingdoms | Catching NullPointerException |
Submissions | ||||
---|---|---|---|---|
Submission Date | Submitter | Organization | Source | |
7 Pernicious Kingdoms | Externally Mined | |||
Modifications | ||||
Modification Date | Modifier | Organization | Source | |
2008-07-01 | Eric Dalci | Cigital | External | |
updated Time of Introduction | ||||
2008-09-08 | CWE Content Team | MITRE | Internal | |
updated Relationships, Other Notes, Taxonomy Mappings | ||||
2009-03-10 | CWE Content Team | MITRE | Internal | |
updated Relationships | ||||
2009-05-27 | CWE Content Team | MITRE | Internal | |
updated Demonstrative Examples | ||||
Previous Entry Names | ||||
Change Date | Previous Entry Name | |||
2008-04-11 | Catch NullPointerException | |||