A NullPointerException is caught instead of checking for null

tx0/java:catch-null-pointerrecommendedwarningsmell, cwe-395

What it finds

catch (NullPointerException e) treats a programming error as an expected outcome: it hides the real defect, a missing check for null, and catches every other dereference of null in the block too.

From the rule's source, catch_null_pointer.rs.

Example from the rule's tests
class Names {  int length(String name) {    try {      return name.length();    } catch (NullPointerException e) {tx0 reports: a NullPointerException is caught      return 0;    }  }   int size(java.util.List<String> names) {    try {      return names.size();    } catch (IllegalStateException | NullPointerException e) {tx0 reports: a NullPointerException is caught      return 0;    } catch (RuntimeException e) {      return -1;    }  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:catch-null-pointer" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:catch-null-pointer] reason="..."

tx0 explain tx0/java:catch-null-pointer shows the same in the terminal. Found a false positive? Report it.