# java.lang.AssertionError: 期望为 null，但实际为：<某个值>

- **ID:** `java/assertion-error-expected-not-null`
- **领域:** java
- **类别:** assertion_error
- **验证级别:** ai_generated
- **修复率:** 78%

## 根因

代码中的断言（通常来自 JUnit 等测试框架或显式的 assert 语句）失败，因为期望为 null 的值实际不为 null，表明空值处理存在逻辑错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| JUnit 4.13 | active | — | — |
| JUnit 5.10 | active | — | — |
| Java 8+ | active | — | — |

## 解决方案

1. ```
   Trace back to the source of the non-null value. For example, if a method is expected to return null but returns a value, add debug logging: System.out.println("Value: " + value); and check the method's logic.
   ```
2. ```
   Use a conditional breakpoint in the IDE (e.g., IntelliJ IDEA) set on the assertion line to inspect variables when the assertion fails.
   ```
3. ```
   If using JUnit, replace assertNull with assertNotNull if the expectation is incorrect, or use assertNull(obj, "message") to add a descriptive message for debugging.
   ```

## 无效尝试

- **Remove the assertion entirely from the code** — This suppresses the symptom but does not fix the underlying null-handling bug; the logic may still produce incorrect results. (95% 失败率)
- **Change the expected value in the assertion to match the actual value** — This bypasses the validation and may hide a real bug where null was expected but a non-null value indicates an error in the system. (90% 失败率)
- **Add a try-catch block to swallow the AssertionError** — AssertionErrors are not meant to be caught; swallowing them violates the intent of the assertion and can lead to silent failures. (85% 失败率)
