Examining program data values

Even though you can discover many interesting things about your program by running and stepping through it, you usually need to examine the values of program variables to uncover bugs. For example, it's helpful to know the value of the index variable as you step though a for loop, or the values of the parameters passed in a method call.

When you pause your program while debugging, you can examine the values of instance variables, local variables, properties, method parameters, and array items.

Data evaluation occurs at the level of expressions. An expression consists of constants, variables, and values in data structures, possibly combined with language operators. In fact, almost anything you use on the right side of an assignment operator can be used as a debugging expression.

JBuilder has several features enabling you to view the state of your program, which are described in the table below.

Debugger features

Feature Enables...
Loaded classes and static data view Viewing classes currently loaded by the program, and the static data, if any, for those classes.
Threads, call stack and data view Viewing the thread groups in your program. Each thread group expands to show its threads and contains a stack frame trace representing the current method call sequence. Each stack frame can expand to show data elements that are in scope.
Data watches view Viewing the current values of variables that you want to track. A watch evaluates an expression according to the current context. If you move to a new context, the expression is re-evaluated for the new context. If it is no longer in scope, it cannot be evaluated.
Evaluate/Modify dialog box Evaluating expressions, method calls, and variables.
ExpressionInsight Viewing the values of expressions.

How variables are displayed in the debugger

Variables can have different scope - there are static, or class variables, local variables, and member variables. A variable can hold a single value, such as a scalar (a single number), or multiple values, such as an array.

Static variable display

Static variables are displayed in the Loaded classes and static data view. When you expand the tree of loaded classes, all static variables defined for a class and their values are displayed. You can use the right-click menu to set watches on these variables, change values of primitive data types, or toggle the base display value.

Loaded classes and static data view

Local and member variable display

Local and member variables are displayed in the Threads, call stacks, and data view. When you expand a thread group, a stack frame trace representing the current method call sequence is displayed. To display member variables, when you are in the class itself, expand the this object. When you expand that node, you see all of the variables that are members of that class and the instantiation that you are working through. Grayed-out elements are inherited.

You can then use the right-click menu to set watches on these variables, and, if the variable is an array, create an array watch and determine how many array elements will display in the view. You can also create a watch for the this object.

Threads, call stacks, and data view

Changing data values

You can use the Data watches view, the Threads, call stack, and data view, and the Loaded classes and static data view to examine and modify data values for variables.

You can directly edit the value of a string or any primitive data type, including numbers and booleans, by right-clicking and choosing Change Value. (This is a feature of JBuilder Professional and Enterprise.)

Changing variable values

To change the value of a variable,

  1. Select the variable whose value you want to modify.
  2. Right-click and choose Change Value.
    The Change Value dialog box is displayed.

  3. Enter the new value. The new value must match the type of the existing value. The dialog box instructions state what type of value is expected. If the type doesn't match, the value will not be changed. A String constant must be surrounded by opening and closing " characters; a char constant value must must be surrounded by opening and closing ' characters.
  4. Click OK.

To change the display base of a numeric variable, right-click and choose Show Hex/Decimal Value. This command is a toggle - if the value is displayed in hex, it will display in decimal and vice versa.

Changing object variables

Object variables can be cut, copied, and pasted into other objects by using the Cut, Copy, and Paste commands on the right-click menus. If an object is pasted into another variable, both variables will point to that same object. (The Cut, Copy, and Paste commands are features of JBuilder Professional and Enterprise.)

Changing variable array values

You can change the value of an array element by right-clicking on the element you want to change and choosing Change Value. See the section called "Changing data values" for more information.

You can also change how the array is displayed. You can view the details of the array by expanding it. However, there might be so many items displayed that you'll have to scroll in the view to see all the values. For easier viewing, you can reduce the number of items shown with the Adjust Range dialog box. By default, only the first 50 elements of an array are displayed.

To reduce the number of array elements displayed,

  1. Right-click the array (the item in the view preceded by   ) and choose Adjust Display Range.
  2. The Adjust Range dialog box is displayed.

  3. Enter the number of array elements you want to see.
  4. Click OK.

You can also hide or display a null value in an array variable. This is useful when debugging a hash-map object. To enable this feature, right-click an array of type Object and choose Show/Hide Null Value. (This is a feature of JBuilder Professional and Enterprise.)

Watching expressions

Watches enable you to monitor the changing values of variables or expressions during your program run. After you enter a watch expression, the Data watches view displays the current value of the expression. As you step through your program, watch expressions will be evaluated when they are in scope.

Watch expressions that return an object or array value can be expanded to show the data elements that are in scope. For example, if you set a watch on a this object, or on a single object, the watch can be expanded. However, if you set a watch on a primitive value, the watch can't be expanded since it's a single item. The grayed-out items in the expanded view are inherited.

Data watches view

You can set two types of watches:

Variable watches

There are two types of variable watches:

Named variable watches

A named variable watch is added on a name - as you move around in your code, whatever variable has the name you selected in the current context will be the one evaluated for the watch. If no variable has the name you select, the debugger will display the following message in the Data watches view:

variable name = <is not in scope>

To add a named watch, use the Add Watch dialog box. To display this dialog box,

Scoped variable watches

A scoped variable watch watches the variable in the scope (or context) in which you created the watch. As you move around in code, only the specific variable's value will be displayed. Scoped variable watches are features of JBuilder Professional and Enterprise.

If the scoped variable watch expression is not in scope, the debugger will display the following message:

variable name = <is not in scope>

When the expression is in scope, the debugger will display its value.

To add a scoped variable watch, choose the variable you want to watch from the Threads, calls stacks, and data view. Then, right-click. The menu allows you to add a scoped watch for the selected type of variable, including:

The following table shows how some of these types of watches are displayed in the Data watches view:

Types of scoped variable watches

Watch types Display Description
Field watch   "addResult"DebugTutorial.Frame1.this.addResult:
double=68.0
The field being watched, addResult, is a primitive type. It is in DebugTutorial.Frame1. Its value is 68.0.
Local variable watch  "valueOneDouble"valueOneDouble:
java.lang.double={java.lang.Double@354}
The local variable being watched is valueOneDouble. It is defined as a Double object.
Object watch  "DebugTutorial.Frame1"<reference>DebugTutorial.Frame1=
{DebugTutorial.Frame1@353}
The object being watched is DebugTutorial.Frame1. The object expands to show data members.
this watch  "this" this:{DebugTutorial.Frame1@353} The current instantiation of DebugTutorial.Frame1. The object expands to show data members for the current instantiation.
Array watch   "valueOneText"<reference>char[]=char[2] The array being watched is called valueOneText. It contains two array elements.
Array component watch   "[0] = '3' The first element of the array valueOneText. It contains the value '3.'

Object watches

An object watch watches a specific Java object.

To add an object watch,

A this object watch watches the current instantiation of the selected object.

Editing a watch

To edit a watch expression, select the expression in the Data watches view, then right-click.

Deleting a watch

To delete a watch expression, select the expression in the Data watches view, then select Remove Watch or press Delete. You can delete all watches by right-clicking in an empty area of the Data watches view and choosing Remove All.

Caution: The Remove All command cannot be reversed.

Evaluating and modifying expressions

You can evaluate expressions, change the values of data items, and evaluate method calls with the Evaluate/Modify dialog box (Run|Evaluate/Modify). This can be useful if you think you've found the solution to a bug, and you want to try the correction before exiting the debugger, changing the source code, and recompiling the program.

To open the Evaluate/Modify dialog box, choose Run|Evaluate/Modify.

Evaluating expressions

To evaluate an expression, enter the expression in the Expression field. If the expression is already selected in the editor, it is automatically entered into the Expression field. Then, click the Evaluate button. You can use this dialog box to evaluate any valid language expression, except expressions that are outside the current scope. If the result is an object, note that the contents of the object are displayed.

Expression evaluation in the Evaluate/Modify dialog box

Evaluating method calls

This is a feature of JBuilder Professional and Enterprise.

The results of a method call can also be evaluated. To evaluate a method call, enter the method and its parameters in the Expression field of the Evaluate/Modify dialog box. Click Evaluate.

In this example, the method return value evaluated to true.

Method evaluation in the Evaluate/Modify dialog box

Modifying the values of variables

This is a feature of JBuilder Professional and Enterprise.

You can change the values of variables during the course of a debugging session to test different error hypotheses and see how a section of code behaves under different circumstances.

When you modify the value of a variable through the debugger, the modification is effective for that specific program run only - the changes you make through the Evaluate/Modify dialog box do not affect your program source code or the compiled program. To make your change permanent, you must modify your program source code in the editor, then recompile your program.

To modify the value of a variable, enter:

variable = <new value>

in the Expression edit box. The debugger will display the results in the Result display box. Note that the result must evaluate to a result that is type compatible with the variable.

You can also modify the value of a variable using these steps:

  1. Open the Evaluate/Modify dialog box, then enter the name of the variable you want to modify in the Expression edit box.
  2. Click Evaluate to evaluate the variable.
  3. Enter a value into the New Value edit box (or select a value from the drop-down list), then click Modify to update the variable.

The expression in the Expression input box or the new value in the New Value box needs to evaluate to a result that is type-compatible with the variable you want to assign it to. In general, if the assignment would cause a compile-time or runtime error, it's not a legal modification value.

For example, assume that valueOneText is a String object. If you enter:

valueOneText=34

in the Expression input field, the following message, indicating a type mismatch, would be displayed in the Results field:

incompatible types; found int; required java.lang.String

You would need to enter:

valueOneDouble="34"

in the Expression input field in order for the expression to be set to the new value.