Monitoring Variables and Expressions


As you step through a program, you can monitor the running values of fields and local variables in the following ways:

  • By holding the cursor over an identifier in the Source Editor to display a tooltip containing the value of the identifier in the current debugging context.

  • By monitoring the values of variables and fields displayed in the Local Variables window.

  • By setting a watch for an identifier or other expression and monitoring its value in the Watches window.

The Local Variables window (shown in Figure 7-6) displays all variables that are currently in scope in the current execution context of the program, and provides and lists their types and values. If the value of the variable is an object reference, the value is given with the pound sign (#) and a number that serves as an identifier of the object's instance. You can jump to the source code for a variable by double-clicking the variable name.

Figure 7-6. Local Variables window


You can also create a more custom view of variables and expressions by setting watches and viewing them in the Watches window.

The Watches window (Alt-Shift-2), shown in Figure 7-7, is distinct from the Local Variables window in the following ways:

  • The Watches window shows values for variables or expressions that you specify, which keeps the window uncluttered.

  • The Watches window displays all watches that you have set, whether or not the variables are in context. If the variable exists separately in different contexts, the value given in the Watches window applies to the value in the current context (not necessarily the context in which the watch was set).

  • Watches persist across debugging sessions.

Figure 7-7. Watches window


Setting a Watch on a Variable, Field, or Other Expression

To set a watch on an identifier such as a variable or field, right-click the variable or field in the Source Editor and choose New Watch. The identifier is then added to the Watches window.

To create a watch for another expression:

  1. Choose Run | New Watch.

  2. In the New Watch dialog box, type an expression that you want evaluated.

    The expression must be written in Java syntax and can include local variables, fields of the current object, static fields, and method calls.

    You can even create an instance of an object and call one of its methods. When doing so, be sure to refer to the class by its fully qualified name.

Monitoring the Object Assigned to a Variable

You can create a so-called fixed watch to monitor an object that is assigned to a variable (rather than the value of the variable itself).

To create a fixed watch:

  1. After starting a debugging session, open the Local Variables window (AltShift-1).

  2. Right-click the variable that you would like to set the fixed watch for and choose Create Fixed Watch.

A fixed watch is added to the Watches window with an icon. Because a fixed watch applies to a specific object instance created during the debugging session, the fixed watch is removed when the debugging session is finished.

Displaying the Value of a Class's toString () Method

You can add a column to the Local Variables and Watches windows to display the results of an object's toString() method. Doing so provides a way to get more useful information (such as the values of currently assigned fields) on an object than the numeric identifier of the object's instance that the Value column provides.

To display the toString() column in one of those windows:

  1. Open the Local Variables window (Alt-Shift-1) or the Watches window (AltShift-2).

  2. Click the button in the upper-right corner of the window.

  3. In the Change Visible Columns dialog box, select the toString() checkbox.

If you do not see the toString() column appear initially, try narrowing the width of the Value column to provide room for the toString() column to appear.


Changing Values of Variables or Expressions

As you debug a program, you can change the value of a variable or expression that is displayed in the Local Variables or Watches window. For example, you might increase the value of an iterator to get to the end of a loop faster.

To change the value of a variable:

  1. Open the Watches window or the Local Variables window.

  2. In the Value field of the variable or expression, type the new value and press Enter.

Displaying Variables from Previous Method Calls

The Call Stack window displays all of the calls within the current chain of method calls. If you would like to view the status of variables at another call in the chain, you can open the Call Stack window (Alt-Shift-3), right-click the method's node, and choose Make Current.

Making a different method current does not change the location of the program counter. If you continue execution with one of the step commands or the Continue command, the program will resume from where execution was suspended.



NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 279

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net