Debugging Your Programs
Syntax and runtime errors
When your program doesn't compile -- or when it throws an exception at runtime -- you need to figure out the meaning and cause of the error messages. To help you, we've created a page explaining some common Java errors.

Logic errors
When your program compiles and runs but isn't doing what it's supposed to be doing, you need to perform some debugging! In other words, you need to step through your program to see how the values of your variables are changing and to discover where your errors are located.

There are several ways to do this:
  1. Step through the problematic code "on paper". Make a table for the key variables, and use it to keep track of their values (as well as any outputs) as you "execute" the statements in the same order that the interpreter would use. This technique -- which is sometimes called manual execution -- is also useful when attempting to understand code that someone else has given you.

  2. Add temporary println statements at various points in the problematic piece of code (e.g., inside each loop. Use these println statements to print the values of the key variables so that you can see how they change over time. Note that you can also print out the value of an expression (e.g., val < 100) to see how it changes over time. If you need to print an array, you can do so using the Arrays.toString() method.

  3. Use the DrJava debugger. You may find it helpful to consult the following documents:

    Don't forget that when you use the debugger to step through a program, you can use the Interactions Pane to print the values of variables or expressions.

These approaches should help you to isolate the problematic portion(s) of your code. If you still aren't able to figure out the problem, write to cs111-staff @ cs . bu . edu with the relevant portion of your code and whatever insights you were able to obtain from the debugging process.