Compiling, running, and debugging tutorial
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.
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.
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
For information on the debugger UI, see "The debugger UI" topic.
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.
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.)
Scroll in the editor so that you can see the breakpoint and the floating window at the same time.
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.
subtractValues()
method. The subtractValues()
method is now at the top of the right pane of the floating Threads, call stacks and data view.
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.
subtractValues()
method.
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.
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.
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.
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.
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.
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.
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.
subtractValues()
method.
valueOneDouble
and valueTwoDouble
.
The two values are equal. You did not enter two equal values into the program's two input fields.
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 subtractStringResult
in the Expression field. Click OK. You may have to scroll the Data watches view to see the watch.
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.
multiplyValues()
, is called.
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.