|
||||||
|
There a several main ways in which one gets a hold of what's going on in a Java program and track down "undesired features":
Note that there are many more capabilities of the debugger than are discussed here! Breakpoints:To set or clear a breakpoint:
When the program is run, execution will stop every time that line of code is encountered. At this point, the values of variables and the return values of methods can be checked. Program execution from that point on can be checked by single-stepping the execution. Clicking the "Run" button (VCR-like button on main toolbar) will resume normal progam execution. It is possible to set a "conditional breakpoint" where execution only halts when a given expression evaluates to true. To set a conditional breakpoint, select "Source/Set Conditional Breakpoint" from the main menu. Tip: Be very careful that you properly identify which instance of an object you have stopped in. Tip: To see what method calls lead up to the present situation, click the "Call Stack" button on the main toolbar. Single Stepping:There are several modes of single-stepping that can be accessed via three buttons on the main toolbar:
Use single stepping in conjuction with watches and evaluations to make sure you know exactly what state your objects are in and that the program logic is correct so that execution progresses in the expected manner. Tip: Another useful option is to execute normally up to the present cursor location. Select this option by right clicking the mouse and selecting "Continue to Cursor". This is helpful to jump over blocks of code. Watches/Evaluations:A "watch" is a display that, whenever the program execution is halted, shows the value of a variable, the return value of a method, or a more general expression that evaluates to a value. To add an expression to the watch list, click on the "Watch" button on the main toolbar, and type the expression into the next available empty line in the window that appears. To delete an expression from the watch list, highlight the desired line and press "Delete". When the program is halted, expressions can be evaluated and their values displayed. An expression can be a variable, a method or Java code that evaluates to a value. Note that methods that set values can be evaluated. Using an expression such as "x=5" can be a handy way of setting an errant variable to its proper value and checking the rest of the program. Be sure to fix why the variable was incorrect in the first place! To evaluate an expression, click on the "Evaluate Expression" button on the main toolbar and type in the desired expression. Tip: All the property variables of an object can be accessed at once by clicking the "Variables" button on the main toolbar.
|