Debugging a Program with JBuilder


Starting the Debugger

To use the debugger, choose

   Project -> Debug Project

At the bottom of the window you should notice some new icons. These will allow us to interact the debugger. We also can use the Run menu as well.

The set of buttons to the left of the output area (bottom most section of the window) controls what will be displayed there. One of the icons there looks like a small computer screen. If we click on this icon we get the console for the Java program. Any output of the program will be here. If we need to type input we will do it here as well. One of the buttons is a small red hexagon. This allows us to view any breakpoints that we have set. A third button is a pair of glasses and lets us view the values of variables that are being watched.

We also have a row of buttons that are the basic controls for the debugger.

  1. The first (leftmost) button is the Smart Step button that allows one to execute a single step in the program. This one will only step into your own code.
  2. The second button is the Step Over button, which is one of the most useful debugging operations. This button lets you execute the program one line at a time, stepping "over" any method calls.
  3. The third button is the Step Into button, which another useful operation. It also allows you to execute your program a statement at a time, but if the statement contains a method call, this button will step "into" that method, letting you trace its execution the same way.
  4. The fourth button is the Step Out button. If you ever step into a method that you wish you hadn't, this operation runs the remainder of that method.
  5. The fifth button is the Add breakpoint button. It allows you to break at a given line number, method, or class.
  6. The sixth button is the Add watch button. It allows you to type in an expression and at each step in the computation examine its value.
  7. The seventh button is the Thread button. It allows you to examine the threads in your program. (We'll discuss threads in the last two labs in more detail.)

Below this are the two controls for the application. The green triangle will allow us to run the application. The red box allows us to stop executing the program.

Use the controls to place a breakpoint on the class TaxTotals. As the code runs you will see a pictograph that moves through the source indicating the line of code that is about to be executed.

Start the execution. You should be at the first line of code in TaxTotals. Begin by using step over to move to the first statement, which is an output statement. Rather than stepping into the call to readInt(), use step over to move to the next statement. The debugger will wait for you to enter the number of employees. Click on the screen button so that the output area is the console and type your input value in there.

Using step over, step through the remainder of the program, one line at a time. If necessary, you can always stop execution and the begin again.

Remember that when you reach the input statement, you need to enter data in the screen window. Enter the data (e.g., 3) and continue stepping. In particular, watch how the program behaves when execution reaches the for loop.

 

Examining Object Values

A nice feature of this debugger is that we can look at the values of variables while the code is running. Click on the Add Watch button. Choose a variable in your program and enter its name into the text box. You can view that variable by clicking on the watch button left of the output area. When a statement executes that changes the value of a given variable, it will change in the display. This allows you to examine the values of your program's variables and see how the execution of a statement affects them.

Note also that when you reach the end of the for loop, step over takes you back to its beginning. Keep stepping through the statements until you understand how a loop causes a sequence of statements to be repeated.

 

Restarting

As was mentioned earlier, you can always restart the debugger at the beginning of the program by choosing clicking the stop icon followed by the start icon.

 

Stepping Into and Out Of a Method

While there are situations where we want to step over a method (such as a module method like readInt() in class Integer), there are other situations where we want to step into the method. For example, to examine the behavior of our computeTotalCost() method, we might want to step into it, rather than over it.

To illustrate, run your program using step over and enter some value less than the luxury tax minimum for cost and a value for rate. Then continue to step over until execution reaches the line where computeTotalCost() is called. If you now step into computeTotalCost(), the line being executed should "jump" from the call in TaxTotals.java to the first line of method computeTotalCost() in Tax.java. Look at the watch display. The objects from the main function cannot be accessed within computeTotalCost() and no value will be displayed for those variables.

You can then step through this method using the step over or step into methods as we have seen before.

Note how the program behaves when execution reaches the if statement: the code that computes "normal" cost is performed, while the code that computes luxury tax is skipped. Continue stepping until you reach the end of the method. When you step again, you should return to the statement where computeTotalCost() was called.

Continue stepping through the program, and when it pauses for input again, this time enter a value greater than the luxury tax minimum for cost (and a value for rate). When the program stops at computeTotalCost() again, step into computeTotalCost() and note that this time control skips the 'normal' part of the if but moves through its luxury tax part.

As we said earlier, if at any time you want to leave a method (this is especially useful if you mistakenly step into a system method), choose the step out operation. This will complete the execution of the method currently executing and stop back in its caller.

 

 


Back to the Lab


Back to the Table of Contents

Back to the Introduction


Copyright 2000 by Prentice Hall. All rights reserved.