Debugging Programs in JBuilder

Home Up Search Java 2 API C++ Resources

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":

  • Breakpoints:  These are lines in the code in where the debugger will stop the normal execution and wait for debugging commands.  Place breakpoints at the beginning of troublesome code sections to gain hold over the program at those points.
  • Single-stepping:  Once the program has been stopped using a breakpoint, the execution can be progressed one line at a time.  This is very useful for making sure that the program progresses as expected.  
  • Watches/Evaluations:  The values of variables and the return values of methods can be checked or monitored.   This is the basic way in which the state of objects can be checked.

Note that there are many more capabilities of the debugger than are discussed here!

Invoking the Debugger

To 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:

  1. Click the shaded vertical bar just to the left of the line of code where you want the breakpoint.  A red "stop sign" will appear and the line of code will be shaded red when the breakpoint is set.

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.

Back to top

Single Stepping:

There are several modes of single-stepping that can be accessed via three buttons on the main toolbar:

  1. Trace Into:  Clicking this button (the one with the dotted arrow pointing into the white square) will cause the execution to proceed to the next executable line of code, even if it is in another file, method or object.   Use this mode if you want to trace every detail of what your program is doing.   Note that this will often cause the program to step into the internal code of the built-in Java packages.
  2. Step Over:  Clicking this button (the one with the dotted arrow pointing over the white square) will cause the execution to proceed to the next executable line in the method.   If the line is the last line of the method, however,  execution will continue at the next executable line of the calling method.   This mode is very useful for stepping over code that you know already works properly.
  3. Run to Cursor:  Clicking this menu item under the "Run" menu will cause the execution to proceed to the line of code at which the cursor is presently positioned.   This is useful for jumping over a block of code you already know runs properly.
  4. Run to end of Method:  Clicking this menu item under the "Run" menu will cause the execution to proceed to the next executable line in the method that called the present method.   This mode is useful for to getting out of a method that you already know executes properly. 

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.

Back to top

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".

 

Back to top