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.
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
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.
Anatomy of a Stack Trace
The Three-Part Rule
Every stack trace gives you exactly three critical pieces of information. Master these and you can debug any crash.
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
Example 1: NullPointerException
Example 2: ArrayIndexOutOfBounds
Example 3: Deeper Call Chain
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.
Visualizing the Call Stack
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
Question 1 of 5
java.lang.NullPointerException: Cannot invoke "String.toUpperCase()"
at TextProcessor.format(TextProcessor.java:28)
at Main.main(Main.java:6)
What is the failing line in this stack trace?
Question 2 of 5
java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10
at Scores.getScore(Scores.java:15)
at Report.generate(Report.java:42)
at Main.main(Main.java:9)
What does this exception type tell you?
Question 3 of 5
java.lang.NumberFormatException: For input string: "hello"
at java.lang.Integer.parseInt(Integer.java:652)
at java.lang.Integer.parseInt(Integer.java:770)
at InputReader.readInt(InputReader.java:11)
at Main.main(Main.java:7)
The top two frames are in java.lang.Integer. Where should you look to fix the bug?
Question 4 of 5
RuntimeException: Service failed
at App.run(App.java:30)
at Main.main(Main.java:5)
Caused by: java.io.FileNotFoundException: config.txt (No such file)
at FileLoader.load(FileLoader.java:18)
at App.run(App.java:27)
What is the root cause of this crash?
Question 5 of 5
java.lang.StackOverflowError
at Factorial.compute(Factorial.java:4)
at Factorial.compute(Factorial.java:4)
at Factorial.compute(Factorial.java:4)
at Factorial.compute(Factorial.java:4)
... (repeated thousands of times)
The same frame Factorial.compute repeats thousands of times. What does this indicate?