CS 112
Spring 2018

Old version

This is the CS 112 site as it appeared on May 11, 2018.

Debugging Tips

Judiciously Use Print Statements

Use strategically-placed print statements to reveal the values of key variables. It is usually helpful to have:

Don’t overdo it! Putting too many print statements can cause too much irrelevant information to be printed, which can be very confusing.

Do a Step-by-Step Trace

Trace through the method or program by hand or by using the Online Java Tutor to visualize the execution of the code. This requires you to engage the code by either tracing it yourself as if you were the computer, or stepping through the execution line-by-line to determine what happens. Either way, you are forced to look at the code meticulously and can often spot errors.

Thoroughly Test the Code

Some problems will only show up after thorough testing. Be sure to check as many cases as you can think of, especially the “edge” cases that may happen rarely but will break your code.

Good rules of thumb include:

The DrJava Interactions Pane is a great tool to test your methods, since you can quickly make many different method calls and view the value that each call returns.

We also encourage you to add test calls to a main() method or other test method, to make it easier for you to repeatedly run a series of tests.

When creating a large program, you should test each method that you write before moving on to the next method. If you wait until you have written many methods, it can be difficult to pinpoint where the problems are.

Become Familiar with Common Errors

Take the time to review the list of common Java errors that we have compiled for you. If you encounter one of these errors, this page may give you enough information to diagnose and fix the bug. If you come across an error that you don’t recognize and don’t know how to solve, post it on Piazza so that you can get help, and so that we can add the error to the list.