Compiling, running, and debugging tutorial
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.
class DebugTutorial.Frame1; line 214; (unverified)
Choose Remove Breakpoint and click the Close button to close the dialog box.
divideValues()
method.
Right-click the breakpointed line, and choose Breakpoint Properties to open the Breakpoint Properties dialog box.
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.
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.
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.
divideValues()
method.
divideResult = (valueOneDoubleResult * valueTwoDoubleResult)
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.
divideResult = (valueOneDoubleResult * valueTwoDoubleResult)
Can you find the error? The divideResult()
method is multiplying values instead of dividing them.
*
operator to /
.