Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Java
  • Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
  • Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
  • Java Hello World with Maven: What the JDK Does and What Maven Adds
  • Java Variables, Keywords and Naming Rules
  • Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
  • Java Primitive Types and Memory: What int x = 5 Really Does
  • Java Strings and Text Blocks: Quotes, Concatenation and parseInt
  • Java Operators: Arithmetic, a++ vs ++a, Precedence, && || and the Ternary
  • Java Type Casting: Widening, Narrowing and Converting Text to Numbers
  • Java Practice Lab: A Cafe Bill with Variables, Operators and Casting
  • Java if, else and switch: Braces, Else-If Ladders and the Modern Switch
  • Java Loops: for, while, do-while and for-each
  • Java break, continue and Nested Loops
  • What's New in Java 21 to 25: Compact Source Files and Sequenced Collections
  • Java Arrays: Indexes, Default Values, Loops and 2D Grids
  • Java Methods and Overloading: Parameters, return and Pass-by-Value
  • Java Scope, Code Blocks and the Call Stack
  • Read User Input in Java with Scanner (and the Leftover Enter Trap)
  • Java Practice Lab: A Gym Weekly Tracker with Loops, Arrays and Methods
  • Debugging Java in IntelliJ: Stack Traces, Breakpoints and a Better AI Prompt
  • Java var: Let Java Infer the Type
  • Push and Pull in IntelliJ: Two Developers, One GitHub Repository, a Rejected Push and a Conflict
  • Java Classes and Objects: One Blueprint, Many Cars
  • Java Constructors and the this Keyword
  • Java Instance Methods, Getters, Setters and toString
  • Java static: Fields, Methods and Blocks Shared by the Whole Class
  • Java Packages and Imports: Every Class Has an Address
  • Java Access Modifiers and Encapsulation: Private Data, Public Methods
  • Java final and Immutable Objects: Assign Once, Share Safely
  • Java Strings as Objects: The String Pool, == vs equals, StringBuilder and printf
  • Java Practice Lab: A Library Book Catalogue with final Fields and StringBuilder
  • Java Inheritance: extends, super and the Order Objects Are Built
  • Java Method Overriding, @Override and the Object Class
  • Java Polymorphism: Upcasting, Dynamic Dispatch and Downcasting
  • Java Composition: Has-A vs Is-A
  • Java Abstract Classes: A Parent That Leaves One Blank
  • Java Interfaces: Contracts and Multiple Inheritance
  • Java Nested, Inner and Anonymous Classes
  • Java Enums with Fields, Methods and switch
  • Java Practice Lab: Payment Processing with an Interface, an Abstract Class and an Enum
  • Java Records: A Whole Data Class in One Line
  • Java Sealed Classes and Interfaces
  • Java Pattern Matching with instanceof and switch
  • Java Record Patterns: Unpacking a Record
  • Java equals and hashCode: Why a HashSet Needs Both
  • From Arrays to Collections: The Java Collections Framework Map
  • Java Wrapper Classes and Autoboxing
  • Java ArrayList: The Stretchy Array
  • Java LinkedList vs ArrayList: Nodes or an Array
  • Java Sets: HashSet, LinkedHashSet and TreeSet
  • Java Sorting with Comparable and Comparator
  • Java Maps: HashMap, LinkedHashMap and TreeMap
  • Java Stacks, Queues and Deques: ArrayDeque and PriorityQueue
  • Choosing a Java Collection: Immutable and Sequenced Collections
  • Java Exception Handling: try, catch and finally
  • Java throw, throws, Checked vs Unchecked and Custom Exceptions
  • Java try-with-resources and Clean Error Handling
  • Java Generics: Type-Safe Containers
  • Java Generic Methods, Bounded Types and Wildcards
  • Java Lambdas and Functional Interfaces
  • Java Built-in Functional Interfaces: Predicate, Function, Consumer, Supplier
  • Java Optional: Saying Maybe Without null
  • Java Streams I: filter, map and reduce
  • Java Streams II: Collectors, groupingBy and flatMap
  • Why Concurrency: Processes, Threads and the Main Thread in Java
  • Creating Threads in Java: Thread, Runnable and Lambdas
  • Java Thread Lifecycle: sleep, join and interrupt
  • Java Race Conditions and synchronized
  • Java volatile and Atomic Variables
  • Java ExecutorService and Thread Pools
  • Java Callable, Future and CompletableFuture
  • Java Locks, Deadlock and tryLock
  • Java Concurrent Collections and Virtual Threads
  • Why Slow Work Belongs off the Request Thread: 202 Accepted and a Job Id in Spring Boot
  • Virtual Threads in Spring Boot: One Property, and the Connection Pool Trap
  • @Async, Named TaskExecutors and Structured Concurrency in Spring Boot
  • Java Files and Directories with NIO.2: Path and Files
  • Reading and Writing Text Files in Java
  • Java Structured Data: Reading and Writing JSON and CSV
  1. Blog
  2. /
  3. Java
  4. /
  5. Basics

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Basics

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 line sum += prices[i];.
  • Line 3, who called it: Main.java:8, where main calls CartCalculator.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 Debug window: a toolbar with Resume, Stop, Step Over, Step Into and Step Out at the top; the blue line in the editor is the line that runs next; Frames on the left list total above main; Variables on the right show prices, sum 800 and i 3. Toolbar: Resume | Stop | Step Over | Step Into | Step Out 8: sum += prices[i]; blue line = runs next Frames total:8 (top) main:8 Variables prices = {250, 400, 150} sum = 800 i = 3 (length is 3)
Paused inside total on the last lap: i is 3, but the only slots are 0, 1 and 2.
  • 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 lapsumi
12501
26502
38003

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

ButtonWhat it doesUse it when
Resumeruns to the next breakpoint or the endyou've seen enough
Step Overruns this line; a method call is one stepyou trust the method
Step Intogoes inside the method on this linethe bug may be inside
Step Outfinishes this method, back to the callerthe 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:

  1. Expected: main should print "Cart total: 800" and "Average price: 266".
  2. Actual: the exception and the full stack trace.
  3. Smallest input that breaks it: prices = {250, 400, 150}.
  4. What the debugger showed: paused on CartCalculator line 8, i is 3, sum is 800, prices.length is 3.
  5. The code: the loop.
  6. 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.

Keep going in Java · 20 of 79

Previous
Java Practice Lab: A Gym Weekly Tracker with Loops, Arrays and Methods
Next post
Java var: Let Java Infer the Type
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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.

On this page

  • The program and the crash
  • The quick route: paste the trace into an AI
  • Read the trace: where, then who called
  • A breakpoint, then Debug instead of Run
  • Step Over vs Step Into
  • Watch sum and i lap by lap
  • Fix it, then Step Out and Resume
  • The four buttons
  • Tell the AI what you saw
  • Recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.