|
||||||
|
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! Invoking the DebuggerTo run a program using the debugger, click the green "play" icon with the "piece of paper" and curved dotted arrow on it (to the right of the regular "play" button). Breakpoints:To set or clear a breakpoint:
When the program is run, execution will stop every time that line of code is encountered. Execution stops before that line is executed. 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 green "play" button (either the main one or the one on the debugger pane) will resume normal progam execution. It is possible to set a "conditional breakpoint" where execution only halts when a given expression evaluates to true or a number of other possibilities. To set a conditional breakpoint, right-click the "stop sign" at the breakpoint and select "Properties". Tip: Not all lines in a program actually execute in a manner such that the program can be halted there. For instance, breakpoints placed on curly braces, try statements, do statements, etc. may not appear to work. 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, look at the upper left pane in the debugger window which shows all the threads and what method call each thread is presently in. 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. Watches: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" tab on the bottom of the left hand pane (the tab with the eyeglasses icon). Right click anywhere in the watch pane and select "add watch". Type the expression into the textfield that appears. To delete an expression from the watch list, right click the item and select "Remove Watch". Evaluations: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, right click the name of the variable in the code and select "Evaluate/Modify". Type in the desired expression. Tip: All the property variables of an object can be accessed by clicking on the "+" sign next to their name to expand the description. Tip: The value of a variable can be seen by simply highlighting it in the code. Objects will tend to show up as "{...}". An object can be "inspected" by highlighting the object's name in the code, and selecting "Run/Inspect" from the main menu. (Note, JBuilder often gets the name of what you want to inspect wrong, so double check it before click on "OK".
|