Home Step 03 Step 05

Compiling, running, and debugging tutorial

Step 4: Fixing the subtractValues() method

In this step of the tutorial, you will find and fix one of three runtime errors. To find this error, you'll use debugger features. You'll learn how to start and stop the debugger; create a floating window for one of the debugger views; set a breakpoint; step into and step over a method; trace into a thread; set a this watch, an object watch, and a local variable watch; and use the Evaluate/Modify dialog box.

In Step 3, you ran the program. When you entered values into the Value 1 and Value 2 input fields, and pressed Compute Values to compute the added, subtracted, multiplied, and divided values, you may have noticed that the subtracted value was not correct.

For example, if you enter 4 in the Value 1 field and 3 in the Value 2 field, the subtracted result is 0.0 instead of 1.0.

To find this error, we'll use the debugger. First, we'll set a breakpoint and start the debugger.

  1. Use the Find/Replace Text dialog box (Search|Find) to find the line of code that calls the addValues() method. This is the first method called when the Compute Values button is pressed. Enter addValues in the Text To Find field of the dialog box to locate the call to the method. Press the Find button.

  2. Click the gray gutter in the editor to the left of the line of code. A breakpoint is set on this line. The red circle    indicates that the breakpoint has not been verified.

  3. Click the Debug Program button    on the toolbar. JBuilder starts the debugger VM.

  4. The program is now running and waiting for user input (this may take a few moments). Enter 4 in the Value 1 field and 3 in the Value 2 field. Press Compute Values. Before you can examine the results, the debugger takes control. The program is minimized and the debugger is displayed in the message pane. Blue glyphs    are now displayed in the editor next to executable lines of code, showing where valid breakpoints can be set. The glyph for the breakpoint you just set has changed to a red dot with a green checkmark    to show that the breakpoint is valid. The arrow indicates the execution point (in this case, the breakpointed line is also the execution point).

    For information on the debugger UI, see "The debugger UI" topic.

  5. Click the Breakpoints tab on the left side of the debugger    to go to the Data and code breakpoints view. The default breakpoint and the breakpoint you just set are displayed. The debugger status bar displays a message indicating that the program has stopped on the breakpoint you set in the editor.

The next step is to trace into the stepping thread. This allows you to see where methods are called and set watches on those methods.

  1. Go to the Threads, call stacks, and data view  . Notice how the view is split, allowing you to see the contents of the item selected on the left side on the right side. (The split view is a feature of JBuilder Professional and Enterprise.)

  2. This step is for JBuilder Professional and Enterprise users only.

    Right-click an empty area of the left side of the view and choose Floating Window. The view now turns into a floating window and is initially displayed at the top left of the screen. You can resize the window or move it. Changing a view to a window allows you to look at more than one debugger view at a time. (Note that all views, except the Console, output, input and errors view, can be turned into floating windows.)

  3. This step is for JBuilder Professional and Enterprise users only.

    Scroll in the editor so that you can see the breakpoint and the floating window at the same time.

  4. You could have set the breakpoint on the call to the subtractValues() method instead of the addValues() method, allowing you to get closer to the actual area of the program you want to examine more closely.

    To do this, click the Step Over button    on the debugger toolbar. This steps over the call to the addValues() method, positioning the execution point on the call to the subtractValues() method.

  5. Click the Step Into button    to step into the subtractValues() method. The subtractValues() method is now at the top of the right pane of the floating Threads, call stacks and data view.

  6. This step is for JBuilder Professional and Enterprise users only.

    Right-click an empty area on the left side of the floating Threads, call stacks and data view and uncheck Floating Window to close it. The floating window is displayed again as a debugger view.

    Tip: If you want to reset the debugger tabs to their default order, right-click an empty area of the view and choose Restore Default View Order.

  7. Go to the Threads, call stacks, and data view and expand the subtractValues() method.

    Tip: You can use the Show Current Frame button    to display only data on the right side of the view.

The next step is to set watches on objects and variables. This allows you to examine data values.

  1. Create a this object watch by right-clicking the this object in the expanded list:

         this = {DebuggerTutorial.Frame1@3c6}

    Choose the Create 'this' Watch command. A watch on the this object allows you to trace through the current instantiation of the class.

  2. The Add Watch dialog box is displayed, with the Enter A Watch Description field available. Click OK.

    You do not need to enter a description for the watch. If you do enter a description, it is displayed on the same line as the watched expression in the Data watches view. A description may make individual watches easier to locate in the view.

  3. Right-click the this object again:

         this = {DebuggerTutorial.Frame1@3c6}.

    This time, choose the Create Object Watch command to create an object watch. The Add Watch dialog box is displayed. Click OK.

  4. Right-click the valueOneDouble object in the expanded list to create a watch on the first value being passed to the subtractValues()method:

         valueOneDouble: java.lang.Double. = {java.lang.Double@3c7}.

    Choose the Create Local Variable Watch command. The Add Watch dialog box is displayed. Click OK.

  5. Right-click the valueTwoDouble object in the expanded list to create a watch on the second value being passed to the method:

         valueTwoDouble: java.lang.Double. = {java.lang.Double@3c7}.

    Choose the Create Local Variable Watch command. The Add Watch dialog box is displayed. Click OK.

  6. Go to the Data watches view   .

  7. Expand the first two watches: the this watch and the <reference> watch. In this case, both the watches provide the same data, as the two watches are identical. Note that you can watch all object data in this view (except static data). The grayed-out items are inherited. Collapse these two watches. The remaining two watches, the local variable watches, watch the values of valueOneDouble and valueTwoDouble.

  8. Click the Step Into button    to step into the subtractValues() method.

  9. Expand the watches on valueOneDouble and valueTwoDouble.

    The two values are equal. You did not enter two equal values into the program's two input fields.

  10. Set a watch on subtractStringResult, the result of the subtraction. This value, a String, is written to the output label. To set the watch, click the Add Watch button    on the debugger toolbar, and enter subtractStringResult in the Expression field. Click OK. You may have to scroll the Data watches view to see the watch.

  11. Click the Step Into button    three times to step to the following line in the editor:

         subtractResultDisplay.setText(subtractStringResult)

    In the Data watches view, subtractStringResult is set to 0.0 instead of 1.0, as expected.

    Note: You could also use the Evaluate/Modify dialog box to examine the value of subtractStringResult. To do this, choose Run|Evaluate/Modify. Enter subtractStringResult into the Expression input field, and click Evaluate. The result of the evaluation is displayed in the Result field. Note that the display is similar to expanding the watch. Click Close to close the dialog box.

  12. Step into the method two more times. The execution point returns to the line where the next method, multiplyValues(), is called.

  13. Look at the call to the subtractValues() method, the line before the execution point. Notice that valueOneDouble is being passed twice, instead of valueOneDouble and valueTwoDouble. Change the second parameter to valueTwoDouble.

Saving files and running the program

To save your changes and run the program,
  1. Click the Save All button    on the toolbar.

  2. Click the Reset Program button    on the debugger toolbar.

  3. Click the Run Project button    on the toolbar. Enter values. The program runs. When you enter values and press the Compute Values button, the subtracted value is now correct. However, if you look carefully at remaining results, you'll see that the divided result is also incorrect. Go to Step 5 to find and fix the error.

  4. Exit the program before you proceed to Step 5. Remove the message pane tabs by right-clicking the Application1 tab and choosing Remove "Application1" Tab.

Home Step 03 Step 05