Debugging Java in IntelliJ: Stack Traces, Breakpoints and a Better AI Prompt
A cart program crashes with 'Index 3 out of bounds for length 3'. Read the stack trace, set a breakpoint, step into the loop, watch sum and i lap by lap, fix <= to <, and turn what you saw into a debugging prompt an AI can act on.
What you will learn
- Read a stack trace top down: what broke, where, and who called it
- Pause a program with a breakpoint and read Frames and Variables in the Debug window
- Choose between Resume, Step Over, Step Into and Step Out
- Write a debugging prompt with expected vs actual, a minimal input and what the debugger showed
Before you start
- Java arrays
- Java scope, code blocks and the call stack
Here's a small cart program that crashes. You'll read its stack trace, pause it with the debugger to see exactly why, fix it, and then turn what you saw into a debugging prompt for an AI assistant.
The program and the crash
package org.studyeasy.s02l18;
public class Main {
public static void main(String[] args) {
int[] prices = {250, 400, 150};
int itemCount = prices.length;
int cartTotal = CartCalculator.total(prices);
int averagePrice = CartCalculator.average(cartTotal, itemCount);
System.out.println("Cart total: " + cartTotal);
System.out.println("Average price: " + averagePrice);
}
}
package org.studyeasy.s02l18;
public class CartCalculator {
public static int total(int[] prices) {
int sum = 0;
for (int i = 0; i <= prices.length; i++) {
sum += prices[i];
}
return sum;
}
public static int average(int total, int count) {
return total / count;
}
}
Click Run and the Run window shows, in red:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at org.studyeasy.s02l18.CartCalculator.total(CartCalculator.java:8)
at org.studyeasy.s02l18.Main.main(Main.java:8)
The process finishes with exit code 1, and nothing was printed.
The quick route: paste the trace into an AI
Copy the trace, paste it into your AI assistant with "fix this", and it will usually say something like: the loop runs one pass too many, so the last pass reads prices[3], which doesn't exist; change <= to <. With current models that's usually right, and often it's enough. When the stakes are high, though, find out why it broke before you accept a fix.
Read the trace: where, then who called
- Line 1, what broke: index 3 in an array of length 3.
- Line 2, where:
CartCalculator.java:8, the linesum += prices[i];. - Line 3, who called it:
Main.java:8, wheremaincallsCartCalculator.total.
Read top down, and start with the first file that's yours. Those two "at" lines are the call stack, printed at the moment of the crash.
A breakpoint, then Debug instead of Run
Click in the gutter next to a line number and a red dot appears: a breakpoint. Now click Debug (the bug button) instead of Run. The program stops on that line, before it runs, and the Debug window opens.
- The blue line is the line that runs next.
- Frames (left) shows who called whom.
- Variables (right) shows every local with its live value.
- The toolbar has Resume, Stop and the step buttons.
With a breakpoint on Main line 8, the Variables pane shows args (an empty String array), prices with 250, 400, 150, and itemCount 3.
Step Over vs Step Into
Step Over runs the current line and moves on. Paused on line 6 (int itemCount = prices.length;), one Step Over runs it, the blue line jumps to line 8 (line 7 is empty) and itemCount appears with 3. Step Over again, on the call to total, and the program crashes with the same trace. Step Over ran total as a single step, so you never saw inside it.
Step Into goes inside the method on the current line. Paused on Main line 8 again, Step Into opens CartCalculator on line 6, int sum = 0;. Frames now shows two entries, total on top of main.
Watch sum and i lap by lap
| After lap | sum | i |
|---|---|---|
| 1 | 250 | 1 |
| 2 | 650 | 2 |
| 3 | 800 | 3 |
Each lap takes two Step Overs (line 8 to 7 and back). After the third lap sum is already the right total, 800, and i is 3. But prices.length is 3, so the only slots are 0, 1 and 2. The check i <= prices.length still lets the loop in, and stepping over line 8 once more reads prices[3] and crashes.
Fix it, then Step Out and Resume
Change line 7 to i < prices.length. Debug again with a breakpoint on line 8 of CartCalculator. When you've seen enough, click the red dot to remove the breakpoint (or the loop stops on it every lap), then Step Out: total finishes and you're back in main on line 8. Step Over shows cartTotal = 800. Resume runs to the end:
Cart total: 800
Average price: 266
Exit code 0. (The average is 266, not 266.67, because average divides two ints.) A second bug is waiting in average: set itemCount to 0 and it throws ArithmeticException: / by zero.
The four buttons
| Button | What it does | Use it when |
|---|---|---|
| Resume | runs to the next breakpoint or the end | you've seen enough |
| Step Over | runs this line; a method call is one step | you trust the method |
| Step Into | goes inside the method on this line | the bug may be inside |
| Step Out | finishes this method, back to the caller | the rest of the method is fine |
Tell the AI what you saw
"My cart code is broken, please fix it" makes the AI guess. After a debugging session you can give it far more. A good prompt has six parts:
- Expected: main should print "Cart total: 800" and "Average price: 266".
- Actual: the exception and the full stack trace.
- Smallest input that breaks it:
prices = {250, 400, 150}. - What the debugger showed: paused on
CartCalculatorline 8,iis 3,sumis 800,prices.lengthis 3. - The code: the loop.
- The question: what is wrong, and what is the smallest fix?
Recap
- Read a trace top down: what broke, where, who called. Start with your own file.
- A breakpoint stops a line before it runs; Frames and Variables show the state.
- Step Over trusts a method, Step Into opens it, Step Out leaves it, Resume runs on.
- Pasting a trace into an AI often works; debugging shows you why, and makes your prompt precise.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
Install JDK 25 (Microsoft Build of OpenJDK) with JAVA_HOME, install IntelliJ IDEA, then point a run configuration at the JDK and the main class and run Hello World.
Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
Open IntelliJ's AI Chat, pick Claude Agent (the agent behind Claude Code) or Copilot and a model, ask in plain words for Main.java to print the date and time, review the change, and run it.
Java Hello World with Maven: What the JDK Does and What Maven Adds
javac compiles HelloWorld.java into bytecode and java runs it on the JVM. Maven adds one pom.xml, the same folders in every project and one build command. Then a real Maven Hello World in IntelliJ.
Java Variables, Keywords and Naming Rules
A variable is a labelled jar that holds one value at a time. Four small programs show declaring, reassigning, the names Java accepts, the keywords it reserves, and why daysUntilLaunch beats d.
