# VNX-395: Use of NullPointerException Catch to Detect NULL Pointer Dereference

A catch clause catches the null-dereference exception (Java NullPointerException, Kotlin NullPointerException/KotlinNullPointerException.

| Field | Value |
|---|---|
| Rule ID | `VNX-395` |
| Severity | Medium |
| SARIF level | `warning` |
| Kind | `sast` |
| Languages | Java, Kotlin, C# |
| CWE | [CWE-395](https://cwe.mitre.org/data/definitions/395.html) |
| CAPEC | [CAPEC-66](https://capec.mitre.org/data/definitions/66.html) |
| ATT&CK | [T1190](https://attack.mitre.org/techniques/T1190/) |
| CWSS vector | `CWSS:1.0/TI:M/AP:RU/AL:S/IC:N/FC:LT/RP:RU/RL:S/AV:L/AS:N/IN:A/SC:A/BI:M/DI:H/EX:M/EC:N/P:C` |
| Tags | `error-handling` `cwe-395` |
| Source | [`internal/sast/rules/vnx-395.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-395.rego) |

## Overview

VNX-395 maps to [CWE-395: Use of NullPointerException Catch to Detect NULL Pointer Dereference](https://cwe.mitre.org/data/definitions/395.html). It reports Java `catch (NullPointerException e)` (including `final`, `java.lang.NullPointerException` and multi-catch `A | NullPointerException e`), Kotlin `catch (e: NullPointerException)` and `catch (e: KotlinNullPointerException)`, and C# `catch (NullReferenceException)` with or without a variable, including `System.NullReferenceException`.

Not reported: throwing `NullPointerException`, `Objects.requireNonNull`, `assertThrows(NullPointerException.class, ...)`, `instanceof` tests, class names that merely contain the word, test files, comments and strings.

## Why this matters

Catching the null-dereference exception hides which reference was null, costs a stack unwind for what should be a branch, and also swallows null-pointer bugs thrown by unrelated code deeper in the call, so a real defect is reported as "anonymous user" or `-1`.

## What gets flagged

```java
try {
    return user.getProfile().getDisplayName().trim();
} catch (NullPointerException e) {          // FLAGGED
    return "anonymous";
}
```

```kotlin
} catch (e: NullPointerException) {          // FLAGGED
```

```csharp
catch (NullReferenceException)               // FLAGGED
```

## Remediation

1. Test the reference for null, or use `Optional`, Kotlin `?.`/`?:`, or C# `?.`/`??`.
2. Validate required arguments at the boundary with `Objects.requireNonNull` or `ArgumentNullException.ThrowIfNull`.

## References

- [CWE-395: Use of NullPointerException Catch to Detect NULL Pointer Dereference](https://cwe.mitre.org/data/definitions/395.html)
- [OWASP Top 10](https://owasp.org/www-project-top-ten/)

## Suppressing this rule

Put a `nosec` comment on the line, or on line 1 to cover the whole file. Use the comment marker of the file's language (`#`, `//`, `--` or `;`):

```text
# nosec vnx-395
// nosec vnx-395
```

Or record an ignore rule: `vulnetix ignore add --rule vnx-395 --reason "..."`. See [Suppressing findings](/sast/guides/suppressing-findings.md).
