Home Step 04 Step 06

Compiling, running, and debugging tutorial

Step 5: Fixing the divideValues() method

In this step of the tutorial, you will find and fix another of three runtime errors. You will set a breakpoint, step into a method, and learn how to use tool tips and ExpressionInsight to locate errors.

In Step 4, you found and fixed an error with the call to the subtractValues() method. Now, when you run the program again, you may notice that the divided result is also incorrect. For example, if you enter 4 in the Value 1 field and and 2 in the Value 2 field, the divided result is 8.0 instead of 2.0.

To find this error, we'll first set a breakpoint, step into the questionable method, and use ExpressionInsight and tool tips to find the error.

  1. Choose Run|View Breakpoints to remove the breakpoint you set in Step 4. In the Breakpoints dialog box, right-click the following breakpoint:

         class DebugTutorial.Frame1; line 214; (unverified)

    Choose Remove Breakpoint and click the Close button to close the dialog box.

  2. Use the Find/Replace Text dialog box to locate the call to the divideValues() method.

  3. Set a breakpoint on this line.

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

    Right-click the breakpointed line, and choose Breakpoint Properties to open the Breakpoint Properties dialog box.

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

    Click the Log Message option. In the Evaluate Expression input field, enter:

         System.out.println("divideValues method reached")

    The message will be written to the Console output, input and errors view when the specified breakpoint is reached. If the Stop Execution option is also selected the program will stop. The dialog box will look similar to this:

    Click OK to close the dialog box.

  6. Click the Debug button on the main toolbar.

  7. Enter 4 in the Value 1 input box and 2 in the Value 2 input box when the program's UI is displayed. Press Compute Values. Remember, before you can examine the results, the debugger takes control. The program is minimized and the debugger is displayed in the message pane.

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

    Go to the Console output, input and errors view. You'll see the message:

         divideValues method reached

    During the development cycle, you can use this feature instead of adding println statements to your code.

  9. Go to the Data and code watches view. Notice that most of the watches are no longer in scope.

  10. Right-click an empty area of the view and choose Remove All.

  11. Click the Step Into button    to step into the divideValues() method.

  12. Click the button three more times, so that you step past the line that reads:

         divideResult = (valueOneDoubleResult * valueTwoDoubleResult)

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

    Position the mouse over the variable divideResult in the editor. A tool tip displaying the value of divideResult pops up. Notice that the value is incorrect. Based on what you entered, the result should be 2.0. However, it is 8.0.

    You can also press the Ctrl key plus right-click the mouse button to display ExpressionInsight. This pop-up window shows the expression name, its type, and its value. If the expression is an object, you can descend into the object members, as well as use a right-click menu to set watches, change values, and change base display values. For example, position the cursor over divideResultDisplay in line 265. Press the Ctrl key plus right-click the mouse button. You will see the members of the JLabel object. As you scroll down, notice the grayed-out items: these are inherited.

    Click in the editor to close the ExpressionInsight window. The window will also automatically close if the cursor is repositioned.

  14. Carefully read this line of source code (line 264):

         divideResult = (valueOneDoubleResult * valueTwoDoubleResult)

    Can you find the error? The divideResult() method is multiplying values instead of dividing them.

  15. To fix the error, change the * operator to /.

Saving files and running the program

To save your changes and run the program,
  1. Remove the breakpoint in the editor.

  2. Click the Save All button on the toolbar.

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

  4. Click the Run Project button on the toolbar. Enter values in the Value 1 and Value 2 input fields. The program runs and the divided value is now correct. However, if you look carefully at the remaining results, you may spot the last error. If you enter an odd number in the Value 1 field, the program incorrectly reports that the value is even. If you enter an even value, the program says it is odd.

  5. Exit the application before you proceed to Step 6. Remove the Application1 tab from the message pane.

Home Step 04 Step 06