NetBeans Debugger- Short Tutorial


Setting BreakPoint
Adding a Watch
Debugging

Local Variables Window

Stopping the Debugger
 

NetBeans IDE provide a debugging tool that will allow to trace the execution of your programs you can debug by setting breakpoints, watches in your code and  run your application in the debugger. Using the debugger tool you can execute your program line by line and examine the value within your application to locate logical errors in your java programs.


1. Setting Breakpoints

 

A breakpoint is a mark in the source code that indicates the debugger to stop when the execution reach it. When your program stops on a breakpoint, you can inspect the current values of the  variables in your program or continue the execution of your program, one line at a time. To set a breakpoint, click on the left margin (in grey) on the line where you want to place the breakpoint. NetBeans highlights the line in red with a red mark on the left margin.

To remove a breakpoint, click on the breakpoint mark on the left, it will toggle the breakpoint off.

 

[Top]


2. Adding a watch


You can monitor the values of variables or expressions during the execution of your program. An easy way to inspect the value of a variable during step-by-step execution of your program is to hover your mouse over the variable, the debugger will display the value of the variable close to where the cursor is placed. Another way to examine the value of a variable is by adding a watch on the variable. To add a watch to a variable or expression, select the variable or expression you want to monitor, then you can do one of the following:

  1. Select New Watch in the Run menu.

 

The New Watch window pop up, click OK  to add the variable to the Watch list.

  1. Right Click and click on New Watch. Click on the OK button in the New Watch Window to add the variable to the watch list.

 

The Watches window displays the list of current watches, their type and  values.

If the Watch window is not visible, choose Debugging>Watches in the Windows menu .

To remove a watch from the list, right click on the watch you want to remove and select Delete.

[Top]


3. Debugging 

There are several ways to start the execution of your java program  in debug mode.

  1. Choose Debug Main Project in the Run menu to execute the program to the the first breakpoint. If you did not set a breakpoint, the program will run until it terminates.
  2. Choose Step Into in the Run menu to run your program to the first line of the main method of your program. At this point you can examine the values of variables and continue the execution of your program.
  3. Choose Run to Cursor in the Run menu to execute your program until the line in the source code where the cursor is currently located.

Once the execution of your program has stopped, you can trace the execution of your program using the following options:

Step Over ( ) - Executes one line of code. If the line is a call to a method, executes the method without stepping into the method's code.

Step Into ( )-  Executes one line of code. If the line is a call to a method, step into the method and stop on the first line of the method.

Step Out () - Executes one line of code. However, if the source code line is in a method, it will execute the remaining source code in the method and returns to the caller of the method.

Run to Cursor () - Executes the program to the line where the cursor is located.

Continue () - Continue the execution of program until the next breakpoint or until the program terminates.
 

[Top]


4. Local Variables Window

The local variables displays the name, data type and values of all the values in the current scope as well as static member variables among others. To activate the Local Variables window, select Debugging > Local Variables in the Windows menu. The debugger allows you to change the value of  a variable in the Local Variable window  and then continue the execution of your program using that new variable's value.

[Top]


5. Stopping the Debugger
 

To stop the debugger, select  Finish Debugger Session in the Run menu ending the execution of your program in debug mode.

[Top]