01 / 20

Reading
Stack Traces

Learn to decode Java stack traces to pinpoint the failing line, identify the exception type, and trace the call chain that led to the crash.

Java

What Is a Stack Trace?

When your Java program crashes, the JVM prints a stack trace: a snapshot of every method that was active at the moment the error occurred. Think of it as a trail of breadcrumbs showing exactly how the program arrived at the crash.

Key Vocabulary

Exception
An object representing an error condition: its type name describes what went wrong.
Message
The human-readable string after the colon that explains why the error happened.
Stack Frame
One "at" line listing a class, method, file name, and line number in the call chain.

Why Stack Traces Matter

Without a stack trace, you would have to guess where the bug is. With one, you can jump directly to the exact line that caused the error. Professional developers read stack traces dozens of times a day.

1
Saves time: go straight to the failing line instead of scanning hundreds of lines of code.
2
Reveals the call chain: see which method called which, exposing unexpected execution paths.
3
Names the problem: the exception type (e.g. NullPointerException) immediately narrows the bug category.

Anatomy of a Stack Trace

java.lang.NullPointerException: Cannot invoke method Exception Type Error Message at com.app.UserService.getName(UserService.java:42) Line Number File Name at com.app.OrderController.process(OrderController.java:87) at com.app.OrderController.handleRequest(OrderController.java:31) at sun.reflect.NativeMethodAccessorImpl.invoke(...) at java.lang.reflect.Method.invoke(...) at com.app.Main.main(Main.java:10) Your Code Library / JVM Read top → bottom ★ Start at the top. The first "at" line with your file name is the crash site.

The Three-Part Rule

Every stack trace gives you exactly three critical pieces of information. Master these and you can debug any crash.

1
Exception Type (first word before the colon): tells you the category of error. E.g., NullPointerException, ArrayIndexOutOfBoundsException.
2
Error Message (text after the colon): tells you why it happened in plain English. E.g., "Index 5 out of bounds for length 3".
3
Top Stack Frame (first "at" line with your file): tells you the exact file name and line number where the crash happened. E.g., Calculator.java:17.

Checked vs Unchecked Exceptions

Checked Unchecked (Runtime)
Detected at Compile time Run time
Must handle? Yes (try/catch or throws) No (optional)
Common examples IOException, FileNotFoundException NullPointerException, ArrayIndexOutOfBoundsException
In stack traces Rare (usually caught) Very common
Typical cause External resources (files, network) Logic bugs in your code

How to Read Step by Step

1
Read the first line. Identify the exception type (before the colon) and the error message (after the colon).
2
Scan the "at" lines from top to bottom. Find the first line that mentions your file name (not a library or JVM class).
3
Note the line number. Open that file and go to that exact line: this is where the error occurred.
4
Read downward. The remaining frames show the method call chain: who called whom. This reveals the path that led to the crash.
5
Combine type + message + line. Now you know what went wrong, why, and where: fix the bug.

Example 1: NullPointerException

java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null at WordCounter.countLetters(WordCounter.java:12) at Main.main(Main.java:5)
What?
NullPointerException: you called a method on a variable that is null.
Why?
The variable str was never assigned a real String value.
Where?
WordCounter.java, line 12: the call to str.length().

Example 2: ArrayIndexOutOfBounds

java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 at GradeBook.getGrade(GradeBook.java:23) at GradeBook.printReport(GradeBook.java:15) at Main.main(Main.java:8)

Example 3: Deeper Call Chain

java.lang.NumberFormatException: For input string: "abc" at java.lang.Integer.parseInt(Integer.java:652) at java.lang.Integer.parseInt(Integer.java:770) ──── library code above, YOUR code below ──── at InputParser.readAge(InputParser.java:19) at Registration.register(Registration.java:34) at Main.main(Main.java:11)

The "Caused by" Chain

Sometimes a stack trace contains a "Caused by:" section. This means one exception was wrapped inside another. The root cause is always the last "Caused by" block.

RuntimeException: Failed to process order at OrderService.process(OrderService.java:55) at Main.main(Main.java:10) Caused by: java.sql.SQLException: Connection refused at DBUtil.connect(DBUtil.java:22) at OrderService.process(OrderService.java:52)

Visualizing the Call Stack

getName() UserService.java:42 ← crash here process() OrderController.java:87 handleRequest() OrderController.java:31 main() Main.java:10 ← entry point Program starts here TOP BOTTOM Stack grows up Read first Read last

Common Exception Types

Exception Meaning Typical Fix
NullPointerException Called method on null reference Add null check before use
ArrayIndexOutOfBoundsException \( \text{index} < 0 \) or \( \text{index} \ge \texttt{array.length} \) Check loop bounds
StringIndexOutOfBoundsException charAt/substring index invalid Validate string length first
NumberFormatException Cannot parse string as number Validate input before parsing
ClassCastException Invalid type cast Use instanceof before casting
StackOverflowError Infinite recursion Add/fix base case

Common Mistakes

✕ Reading Bottom-Up
The crash site is at the top, not the bottom. Always start reading from the first line.
✕ Fixing Library Code
If the top frame is in a JDK or library class, the bug is still in your code. Scan down to find your file name.
✕ Ignoring the Message
The text after the colon often tells you exactly what is null or which index was bad. Read it.

Question 1 of 5

What is the failing line in this stack trace?

Question 2 of 5

What does this exception type tell you?

Question 3 of 5

The top two frames are in java.lang.Integer. Where should you look to fix the bug?

Question 4 of 5

What is the root cause of this crash?

Question 5 of 5

The same frame Factorial.compute repeats thousands of times. What does this indicate?