Java 7 for Absolute Beginners

(nextflipdebug5) #1

CHAPTER 11 ■ DEBUGGING WITH ECLIPSE


Figure 11-1. Fireworks problem

As you can see, I accidentally created searchlights rather than fireworks. The question is: How did I
do it? Since the answer didn't just jump out at me when I first read the code, I used the debugger to
puzzle out the problem. As always, 99% of the effort went into finding the problem. Once you reach the
“Aha!” moment, actually fixing the problem usually takes very little time (except for those times when
you discover that you need to rewrite the whole program—those are bad days.)
Before we can analyze what went wrong, though, you need to learn more about debugging in
general and how to use the Eclipse debugger in particular, so we'll return to this busted Fireworks
program near the end of the chapter.

The Flow of Debugging


Before we dive into the Eclipse debugger, I think it would be a good idea to present a walkthrough of the
overall debugging process.


  1. Identify the problem. That sounds trivial, but it's often not. In complex
    systems (and sometimes even in simple ones), just stating the trouble can be
    tricky. Ask yourself: Exactly which behavior is wrong?

  2. Set a breakpoint at a line you think (you often can't be entirely sure) will be
    executed before your problem occurs. As you gain skill with debugging, you'll
    get better at setting your initial entry point.

  3. Step over the lines until you hit the error. The line you just stepped over is the
    problem line (or possibly one of multiple problem lines, if you have nested
    issues). If it's helpful, examine the values of the variables as you go. That can
    be especially useful in loops.

Free download pdf