Priya Nair·
Decoded a 40-frame Java trace to the exact NPE line and gave me the regression test
Parse and explain complex stack traces, correlate with source code, and provide a step-by-step fix for the underlying error.
Stack Trace Decoder
You are a stack trace analysis expert. Decode this stack trace and provide a complete diagnosis.
**Stack Trace:**
```
{{stack_trace}}
```
**Application Language/Framework:**
{{language_framework}}
**Relevant Source Code:**
```
{{source_code}}
```
**Runtime Environment:**
{{runtime_env}}
**Steps to Reproduce (if known):**
{{repro_steps}}
Analyze and output:
1. **Crash Classification**: What type of error is this? (NPE, OOM, segfault, deadlock, etc.)
2. **Fault Line Identification**: Exact file, function, and line causing the crash
3. **Call Chain Narrative**: Explain the execution path in plain English
4. **Root Cause Analysis**: Why did this actually happen?
5. **Code Fix**: Provide corrected code with inline comments explaining the fix
6. **Prevention**: Suggest defensive coding patterns to prevent this class of error
7. **Regression Test**: Provide a test case that would catch this bug
Be precise. Quote exact function names and line numbers from the stack trace.
Ergebnisse
1. **Crash classification:** `NullPointerException` — dereferencing a null return value.
2. **Fault line:** `OrderService.applyDiscount(OrderService.java:142)`, frame 0 of the trace, on `coupon.getRate()`.
3. **Call chain:** `CheckoutController.submit` → `OrderService.calculateTotal` → `applyDiscount`. The controller passes the raw `couponCode`; `couponRepository.findByCode(code)` returns `null` for an unknown code, and line 142 calls `.getRate()` on that null.
4. **Root cause:** `findByCode` is a JPA derived query that returns `null` (not `Optional`) on no match, and the caller never null-checks before use — so any invalid coupon code crashes checkout.
5. **Fix:**
```java
// Optional makes the "not found" case impossible to ignore
Coupon coupon = couponRepository.findByCode(code)
.orElseThrow(() -> new InvalidCouponException(code)); // 400, not 500
double rate = coupon.getRate();
```
6. **Prevention:** return `Optional<Coupon>` from all repository finders; enable `@NonNull` analysis so the compiler flags unchecked dereferences.
7. **Regression test:**
```java
@Test
void submit_withUnknownCoupon_returns400() {
assertThrows(InvalidCouponException.class,
() -> orderService.applyDiscount(order, "DOES-NOT-EXIST"));
}
```
Modell: Claude Sonnet 4
30 Likes13 SavesScore: 25
2 Kommentare
Lena Fischer·
The debugging, testing details here are spot on.
Ryan Mitchell·
The observability hooks are a thoughtful touch.