How to debugging in Netbeans Java

IB Teguh TM
4 min readMar 21, 2020

--

Debugging is the process of finding and resolving defects or problems within a computer program that prevent correct operation of computer software or a system.Debugging tactics can involve interactive debugging, control flow analysis, unit testing, integration testing, log file analysis, monitoring at the application or system level, memory dumps, and profiling. Debug in Java can be done in 2 ways, that is the terminal method with jdb and IDE. In this article using Netbeans 11. 3 as IDE. This article from Programming Notes by Chua Hock-Chuan (ehchua@ntu.edu.sg) with some update.

Step 0: Write a Java Program

The following program computes and prints the factorial of n (=1*2*3*...*n). The program, however, has a logical error and produce a wrong answer for n=20 ("The Factorial of 20 is -2102132736" - a negative number?!)

/** Compute the factorial of n */
public class Factorial {
// Print factorial of n
public static void main(String[] args) {
int n = 20;
int factorial = 1;

// n! = 1*2*3...*n
for (int i = 1; i <= n; i++) {
factorial *= i;
}
System.out.println("The Factorial of " + n + " is " + factorial);
}
}

Let us use the graphic debugger to debug the program.

Step 1: Set an initial Breakpoint

A breakpoint suspends program execution for you to examine the internal states of the program. Before starting the debugger, you need to set at least one breakpoint to suspend the execution inside the program. Set a breakpoint at main() method by clicking on the left-margin of the line containing main(). A red circle or an inverted Triangle appears in the left-margin indicating a breakpoint is set at that line.

How to set Breakpoint in Netbeans

Step 2: Start Debugging

Right click anywhere on the source code ⇒ “Debug File”. The program begins execution but suspends its operation at the breakpoint, i.e., the main() method.

As illustrated in the following diagram, the highlighted line (also pointed to by a green arrow) indicates the statement to be executed in the next step.

debug running in Netbeans

Step 3: Step-Over and Watch the Variables and Outputs

Click the “Step Over” button (or select “Step Over” in “Debug” menu) to single-step thru your program. At each of the step, examine the value of the variables (in the “Variable” panel) and the outputs produced by your program (in the “Output” Panel), if any. You can also place your cursor at any variable to inspect the content of the variable.

panel for debug

Step-into-function mean the step into function and Step-out-function go out from function. If want debug to some line use Run-to-Cursor direct to line

Variable appear from

Single-stepping thru the program and watching the values of internal variables and the outputs produced is the ultimate mean in debugging programs — because it is exactly how the computer runs your program!

Step 4: Breakpoint, Run-To-Cursor, Continue and Finish

As mentioned, a breakpoint suspends program execution and let you examine the internal states of the program. To set a breakpoint on a particular statement, click on the left-margin of that line (or select “Toggle Breakpoint” from “Run” menu).

“Continue” resumes the program execution, up to the next breakpoint, or till the end of the program.

“Single-step” thru a loop with a large count is time-consuming. You could set a breakpoint at the statement immediately outside the loop (e.g., Line 11 of the above program), and issue “Continue” to complete the loop.

Alternatively, you can place the cursor on a particular statement, and issue “Run-To-Cursor” to resume execution up to the line.

“Finish” ends the debugging session. Always terminate your current debugging session using “Finish” or “Continue” till the end of the program.

--

--

IB Teguh TM

Odoo Developer with a knack for Python and ERP solutions. Passionate about optimizing business processes and delivering scalable, high-performance systems.