# java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "answer" is null

- **ID:** `java/mockito-when-thenreturn-null-pointer`
- **Domain:** java
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Mockito.when() called with a method that returns null, or the mock object is not initialized.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 8+ | active | — | — |

## Workarounds

1. **Initialize mocks explicitly in @BeforeEach** (95% success)
   ```
   @BeforeEach
void setUp() {
    MockitoAnnotations.openMocks(this);
}
   ```
2. **Use mock() method directly** (90% success)
   ```
   MyService service = mock(MyService.class);
when(service.getData()).thenReturn("test");
   ```

## Dead Ends

- **Adding @Mock annotation without MockitoAnnotations.openMocks()** — Annotations not processed, mock remains null. (70% fail)
- **Using when().thenReturn() on a final method without mockito-inline** — Mockito cannot mock final methods by default, causing NPE. (60% fail)
