As you already know, when someone writes code in a high-level language there are two types of errors that he or she might make—syntax errors and logic errors. NetBeans IDE provides all the necessary tools to help you debug your programs and find the errors.

Debugging syntax errors

Fortunately, NetBeans IDE detects syntax errors while you are typing and underlines them with a wavy red line as shown in Figure 1.

10

Figure 1 In the NetBeans IDE, syntax errors are underlined with a wavy red line

All you have to do is correct the corresponding error and the red line will disappear at once. However, if you are not certain about what is wrong with your code, you can just place your mouse cursor on the red exclamation mark. NetBeans IDE will try to help you by showing a popup window with a brief explanation of the error, as shown in
Figure 2.

11

Figure 2 The NetBeans IDE shows an explanation of a syntax error

Debugging logic errors by executing programs step by step

Compared to syntax errors, logic errors are more difficult to find. Since the NetBeans IDE cannot spot and underline logic errors, you are all alone! Let’s look at following Java program, for example. It prompts the user to enter two numbers and calculates and displays their sum. However, it contains a logic error!

public static void main(String[] args) throws java.io.IOException {
  java.io.BufferedReader cin = new java.io.
          BufferedReader(new java.io.InputStreamReader(System.in));
  double s, a, b, S;

  s = 0;
  System.out.print("Enter 1st value: ");
  a = Double.parseDouble(cin.readLine());
  System.out.print("Enter 2nd value: ");
  b = Double.parseDouble(cin.readLine());
  S = a + b;
  System.out.println("The sum is: " + s);
}

If you type this program into the NetBeans IDE, you will notice that there is not even one exclamation mark indicating any error. If you execute the program (SHIFT+F6), however, and enter two values, 5 and 2, you can see for yourself that even though the sum of 5 and 2 is 7, NetBeans IDE insists that it is zero, as shown in Figure 3.

12

Figure 3 Viewing the result of a logic error in the Output window

What the heck is going on? Of course for an expert programmer, correcting this error would be a piece of cake. But for you, a novice one, even though you are trying hard you find nothing wrong. So, where is the error?

Sometimes human eyes get so tired that they cannot see the obvious. So, let’s try to use some magic! Let’s try to execute the program step by step using the debugger. This gives you the opportunity to observe the flow of execution and take a closer look at the current values of variables in each step.

Place the cursor at the line where the statement s = 0 exists and hit the CTRL+F8 key combination. A pink rectangle appears in the gray margin and the corresponding line has pink background highlighting it as shown in Figure 4.

13

Figure 4 Using the debugger in the NetBeans IDE

Now, start the debugger by selecting “Debug  Debug File” from the main menu or by hitting the CTRL+SHIFT+F5 key combination. By doing this, you enable the debugger and more icons are displayed on the toolbar.

netbeans_debug_toolbar2

The program counter (the green arrow green_arrow icon in the gray margin) stops at the first line of the program as shown in Figure 5.

14

Figure 5 Using the debugger in the NetBeans IDE

Notice: The program counter shows you, at any time, which statement is the next to be executed.

Click on the “Step Over” step_over_netbeans_icon  icon on the toolbar. This action tells the Java interpreter to execute the statement s = 0, and the program counter moves to the second Java statement, which is the next to be executed.

From the main menu, select “Window Debugging Variables”. In the window that appears, you can watch all the variables declared in main memory (RAM) and the value they contain during each step of execution. Until now, the only variable declared and assigned a value is the variable s as shown in Figure 6. 15

Figure 6 The current value of variable s is displayed in the Variables window of the debugger

Click on the “Step Over” step_over_netbeans_icon  toolbar icon again. The second statement is executed. You can go back to the “Output” window and see the output result (see Figure 7).

16

Figure 7 Viewing a prompt in the Output window

Click on the “Step Over” step_over_netbeans_icon  toolbar icon again. The third Java statement is executed and the program waits for you to enter a value. Place the cursor inside the “Output” window, type the value 5, and hit the “Enter ↵” key.

The program counter moves to the fourth Java statement. If you go back to the “Variables” window you can see that, at the moment, two variables (a and s) are declared in the main memory (RAM), as shown in Figure 8.

17

Figure 8 The values of variables s and a are displayed in the Variables window of the debugger

Click on the “Step Over” step_over_netbeans_icon  toolbar icon twice. This action executes the fourth and fifth Java statements. Place the cursor inside the “Output” window, type the value 2, and hit the “Enter ↵” key.

The program counter moves to the sixth Java statement. If you go back to the “Variables” window you can see that, at this moment, three variables (s, a and b) are declared in the main memory (RAM) as shown in Figure 9.

18

Figure 9 The values of variables s, a and b are displayed in the Variables window

Click on the “Step Over”  toolbar icon once again. The statement S = a + b is executed. What you expected here was the sum of 5.0 and 2.0, which is 7.0, to be assigned to variable s. Instead of this, a new variable S is declared in the main memory (RAM) and the value 7.0 is assigned to it, as shown in Figure 10.

19

Figure 10 All declared variables are displayed in the Variables window of the debugger

Now it becomes more obvious to you! You mistakenly declared two variables in the main memory (RAM), the variable s and the variable S with a capital S. So, when the flow of execution goes to the last statement, System.out.println("The sum is: " + s) the value 0 instead of the value 7 is displayed.

You have just found the error! Click on the “Finish Debugger Session” finish_debugger_netbeans toolbar icon to cancel execution, correct the error by changing variable S to s, and you are ready! You just performed your first debugging! Re-execute the program and you will now see that it calculates and displays the sum correctly.

Debugging logic errors by adding breakpoints

Debugging step by step has a big disadvantage. You have to click on the “Step Over” step_over_netbeans_icon  toolbar icon again and again until you reach the position where the error might be. Can you imagine yourself doing this in a large program?

For large programs there is another approach. If you suspect that the error is somewhere at the end of the program there is no need to debug all of it right from the beginning. You can add a marker (called a “breakpoint”) where you think that the error might be, execute the program and when flow of execution reaches that breakpoint, the flow of execution will pause automatically. You can then take a closer look at the current values of the variables at the position where the program was paused.

Notice: When the program pauses, you have two options for resuming the execution: you can add a second breakpoint somewhere below in the program, click on the “Continue”  toolbar icon, and allow the program to continue execution until that new breakpoint; or, you can just use the “Step Over” step_over_netbeans_icon  toolbar icon and execute the program step by step thereafter.

The next Java program prompts the user to enter two values and calculates their average value.

However, the program contains a logic error. When the user enters the values 10 and 12, the value 16, instead of 11, is displayed.

public static void main(String[] args) throws java.io.IOException {
  java.io.BufferedReader cin = new java.io.
          BufferedReader(new java.io.InputStreamReader(System.in));
  double a, b, average;

  System.out.print("Enter 1st value: ");
  a = Double.parseDouble(cin.readLine());
  System.out.print("Enter 2nd value: ");
  b = Double.parseDouble(cin.readLine());
  average = a + b / 2;
  System.out.println("The average value is: " + average);
}

You suspect that the problem is somewhere at the end of the program. However, you do not want to debug the entire program, but just the portion in which the error might be. So, let’s try to add a breakpoint at the average = a + b / 2 statement (see Figure 11). There are two ways to do this: you can click in the left gray margin on the corresponding line number, or you can place the cursor at the line of interest and hit the CTRL+F8 key combination.

20

Figure 11 Adding a breakpoint to a program

Notice: You know that a breakpoint has been set when the pink rectangle ping_rectangle_netbeans appears in the gray margin and the corresponding line has pink background highlighting it.

Hit CTRL+SHIFT+F5 to start the debugger. Enter the values 10 and 12 when requested in the output window. You will notice that just after you enter the second number and hit the “Enter ↵” key, the flow of execution pauses at the breakpoint. (You will see that the pink breakpoint highlighting is replaced by the green highlighting of the program counter.)

Remember! You can debug a file by selecting “Debug Debug File” from the main menu or by hitting the CTRL+SHIFT+F5 key combination.

Now you can take a closer look at the current values of the variables. Variables a and b contain the values 10.0 and 12.0 respectively, as they should, so there is nothing wrong with the data input, as shown in Figure 12.

21

Figure 12 Viewing the current values of variables a and b in the Variables window

Click on the “Step Over”  toolbar icon once. The statement average = a + b / 2 executes and the main memory (RAM) now contains the following values (see Figure 13).

22

Figure 13 Viewing the current values of variables a, b and average in the Variables window

There it is! You just found the statement that erroneously assigns a value of 16.0, instead of 11.0, to the variable average! And now comes the difficult part; you should consider why this happens!

After two days of thinking, it becomes obvious! You had just forgotten to enclose a + b inside parentheses; thus, only the variable b was divided by 2. Click on the “Finish Debugger Session” finish_debugger_netbeans toolbar icon, remove all breakpoints, correct the error by enclosing a + b inside parentheses and you are ready! Re-execute the program and see now that it calculates and displays the average value correctly.

Notice: You can remove a breakpoint the same way you added it: click in the left gray margin on the corresponding line number, or place the cursor at the line that contains a breakpoint and hit the CTRL+F8 key combination.