You press Run and the console fills with red text, starting with a line you did not write. That message is not a verdict on your ability; it is a location and a description. Reading it in the right order resolves most of the failures that halt a Java program, and it resolves them faster than changing code at random.
Start where the exception happened
Read a stack trace from the top. The first line names the exception and often includes a message; the frame beneath it points at a file and line in your code. Library frames can be skipped — scan down until you see your own package name.
If the message reports a NullPointerException on line 42, something on that line is null. Print the object before calling a method on it, or guard the call. Recent Java versions usually name the variable in the message, which shortens the search to a glance.
When == says two equal strings differ
For objects, == compares references while equals compares contents. Two strings built separately can hold identical characters and still fail ==. Use equals for values, and put the literal first, as in "yes".equals(answer), so a null answer returns false instead of throwing.
Watch for mutable state shared between classes
A getter that returns the internal list hands every caller permission to modify it. Bugs of that kind move when you chase them, because the damage happens far from the cause. Return a copy or an unmodifiable view, and keep the collection private.
Check the bounds, then check the build
One iteration too many or one too few appears as a missing last item or an index out of bounds. Look at where the index starts and whether the comparison uses a less-than or less-than-or-equal sign. Printing the index on the first and last pass settles the question faster than reasoning about it.
When a class that clearly exists cannot be found, suspect the build path, a stale output folder, or two versions of the same library. Clean and rebuild before rewriting imports. If a run seems to use old code, the compiled files are what you are running, not the source you just edited.
A five-minute routine
- Reproduce the failure with the smallest input that still breaks.
- Write down the expected value and the actual value.
- Change one line, rerun, and compare the two.
- Add a test that fails before the fix and passes after it.
That last step is what turns a fixed bug into one that stays fixed when the code changes next month.
Errors stop feeling like interruptions once you treat them as the most precise feedback the computer offers. Each one names a file, a line and a wrong value; your task is to find where that value came from. Do it often enough and reading the trace becomes faster than the original mistake.