A Failed Attempt Worth Learning From
A few days ago, an indie developer friend complained to me: he pasted a 200+ line Java error stack trace to an AI, and the reply he got was — "This error is usually caused by a null pointer. Please check null handling in your code."
No kidding. The first line of the stack trace said NullPointerException. What he really wanted to know was: which line in his project actually caused the error — not a textbook explanation of the cause.
You've probably run into this too. The AI clearly has strong code comprehension, but you paste the stack trace, hit enter, and the answer sounds like it's reading documentation aloud. The problem isn't the model — it's how you're using it. Below I'll lay out the common failure patterns and the correct approach, so you can see how many you've stepped on yourself.
Three Common Failure Patterns
Failure pattern one: pasting only the stack trace, no code.
Most frames in a stack trace belong to frameworks and third-party libraries. If the model can't see your business code, it can only speak in generalities about framework class names. It doesn't know what's in your OrderService.java:147, so naturally it can only "suggest checking for nulls."
Failure pattern two: pasting the entire project / entire file and letting the AI find it on its own.
The context gets stuffed full, and the key information gets diluted. The model goes hunting for a needle in thousands of lines of code, attention scattered, and finally picks a spot that "looks plausible" — with questionable confidence, so you can't quite trust it.
Failure pattern three: asking too many things at once.
"Help me locate this error, and also figure out why it times out, and also explain what this warning in the log means." Three questions jumbled together, and the AI half-answers each one, forcing you to follow up on every single one.
What's the common thread? You didn't give the AI enough focused context, and you didn't give it a clear task boundary.
The Right Approach: A Three-Step Feeding Method
For error localization, the AI needs three things: the stack trace, the suspect code, and runtime environment information. The process breaks into three steps:
Step one: have the AI "slim down" the stack trace first.
Don't ask the AI for an answer directly — first have it filter out the frames that belong to your own project's package name and list them in call order. The output of this step is a few file names and line numbers. The cost is minimal, but it instantly narrows the scope from dozens of frames to three or five spots.
Step two: based on the line numbers the AI gives, feed back the suspect code segments (10-20 lines before and after) along with the relevant type definitions.
Now the model isn't looking at an abstract NPE, but can reason concretely: "line 147's order.getCustomer().getLevel(), where getCustomer() may return null."
Step three: have the AI produce a three-part output — "localization + evidence + fix suggestion" — and explicitly require it to flag uncertainty.
Require it to state clearly: which file and which line the problem is in, what the reasoning chain is, and where its uncertainty lies if the evidence is insufficient. This way you can quickly verify the answer instead of blindly trusting it.
A Reusable Prompt Template
Save the template below and just replace the parts in angle brackets:
你是一名资深后端工程师,帮我定位一个报错。
【项目包名】<例如:com.myapp>(用于区分业务代码和框架代码)
【报错堆栈】
<paste完整堆栈,从异常类型那行开始,往下贴到最底层的Caused by>
【嫌疑代码】
<第一步AI给出的文件和行号,贴出该位置前后约20行代码;如有相关类定义一并贴上>
【环境信息】
- 语言/框架版本:<例如 Java 17 / Spring Boot 3.2>
- 触发场景:<例如:用户提交订单时偶发,非必现>
- 最近的改动:<例如:上周重构了优惠券模块>
请按以下格式回答:
1. 定位结论:最可能出问题的文件、行号、具体语句
2. 推理过程:从堆栈到该行代码的完整证据链,逐帧解释
3. 不确定项:如果证据不足,明确列出你不确定的地方,以及还需要我提供什么信息
4. 修复建议:给出最小改动的修复方案,并说明潜在副作用
注意:不要泛泛解释异常类型的常见成因,所有结论必须落在具体代码行上。That last constraint line is crucial — it blocks 80% of "textbook-style replies."
Before and After Comparison
Typical conversation, before:
> Me: paste 200 lines of stack trace →
> AI: "NullPointerException usually occurs when an object is null. Consider adding null checks..."
> Me: follow up three times, adding a bit more info each time, twenty minutes of back-and-forth, and in the end I found it myself with a debugger breakpoint.
Typical conversation, after:
> Me: stack trace + template →
> AI: "The stack contains 3 frames belonging to com.myapp; the deepest business code is in OrderService.calculateDiscount, line 147. Please provide lines 130-160 of that file."
> Me: paste the code →
> AI: "Line 147: coupon.getRule().getThreshold(). The stack trace shows the coupon comes from cache deserialization; when stale data in the cache is missing the rule field, this value is null. The trigger scenario matches your 'refactored the coupon module last week.' Suggest adding a null check and version-compatible cache handling; side effects are..."
Convergence to a specific line of code within two follow-ups, with an evidence chain you can go verify directly. Based on my own experience, localization time for common errors dropped from half an hour at minimum to just a few minutes — the exact numbers vary by project, but the order-of-magnitude change is certain.
A Few Additional Tips
- Don't skip the environment info. "Intermittent or always reproducible" and "what changed recently" are often the keys to cracking the case; the accuracy of the AI's reasoning depends heavily on them.
- Let the AI say "I don't know." The template forces an uncertainty section in the output, which effectively prevents it from confidently guessing the wrong line.
- Cross-validate with multiple models. For tricky problems, you can send the same template to two different models; when their conclusions agree, credibility rises sharply. If you're using a unified gateway service to manage multiple models, the switching cost of this kind of cross-validation is nearly zero — see the official pricing page for specific rates.
Final Thoughts
Error localization is one of the scenarios that best illustrates "how you use AI determines the quality of output": the same model, given a pile of unstructured info, produces an encyclopedia entry; given structured input, it produces an answer specific to the line. Make the template above a fixed first step in your troubleshooting workflow, and after a few tries you'll feel the difference.
If you don't yet have a convenient way to access models, try ThisToken's AI Gateway — one API Key to call multiple mainstream models, which makes cross-validating error localization conclusions very convenient: https://api.thistoken.ai/register
---
Tired of juggling provider integrations? Register at https://api.thistoken.ai/register and call every model through one base_url.
Bạn muốn thử Token.AI?
Tạo API Key cấp dự án, bật kênh trong bảng điều khiển và định cấu hình định tuyến, ngân sách và nhật ký kiểm tra.
注册 ThisToken.AI 并获取 API Key