Debugging with NetBeans

To debug a Java program in NetBeans, you just select a project to debug or the class file itself, as shown in the following screenshot:

Debugging with NetBeans

To debug line-by-line, you should add a breakpoint. So, you can place a breakpoint by clicking on the corresponding line number. Let's add a breakpoint in the beginning of the main method.

Debugging with NetBeans

Use the following commands to debug line-by-line on the source code:

  • F5: This is used to step into the method
  • F6: This is used to step over the method
  • F7: This is used to return to the step
  • F8: This is used to resume debugging
  • Ctrl + F2: This is used to terminate the debug

To inspect the value of a variable, just right-click on the code screen and select the New Watch option (or just press Ctrl + Shift + F7). Insert the name of the variable or expression you want to watch and click on Ok. You can see at the bottom of the screen a section called variables, where all the user custom expressions and relevant variables are displayed with their current values, as shown in the following screenshot:

Debugging with NetBeans

In the preceding case, n is an object, so you can expand all its attributes by clicking on the + sign on the left-hand side. And there you are; all the attributes are shown as follows:

Debugging with NetBeans
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset