This tutorial describes techniques about how to debug your program and figure out what is wrong with it. First we will start off with some terminology about debugging, and then I will show some of the debugging features Netbeans has to offer.


Part 1: Terminology


Part 2: Features


Part 3: Brief Introduction

For a brief introduction, we will put a breakpoint inside a method, run Netbeans in debug mode and take a look at the variables at a breakpoint. First, use the Tutorial project from the previous tutorials and place a breakpoint inside the printName method of the Person class. You can place a breakpoint by clicking on the left of that line. You will notice a pink square will appear where you click and the whole line will be highlighted pink. Clicking on a breakpoint you have already set will deactivate it.

break

To start debugging, press the "Debug Main Project" button. It is the button that is highlighted red in the picture below.

btn

After running it in debug mode, you will notice the program will pause at that line. You will know it is paused because the line will now be colored green. Additionally, if you look at the call stack at the bottom of the screen, you will notice the list of methods that lead to this breakpoint. The main method in the Driver class called printName in the Person class.

breakhit

Now that we are at a breakpoint, we can watch any variable available to us at this moment. Unfortunately, this is a boring tutorial program and the only variable available to us is the "name" variable. Click on the "Watches" tab at the bottom. Now click on Run > New Watch at the top menu. Type in "name" to watch the name variable and press the OK button.

watch

After pressing enter you will notice that the name variable is in the Watches window. You will also notice that the value for this variable at this current moment is "Mike".

watchwindow

You can also achieve the same effect by just mousing over the "name" variable when that breakpoint is reached. See the picture below.

mouseover

When you are done, you can press the continue button at the top. The program will finish executing as normal.

continue

This feature is meant to help you figure out what the values of variables are without having to put a lot of print statements all over your program. Of course there are many more advanced debugging features such as step-ins, step-outs, bookmarking, etc... You can learn more about debugging by searching it on the internet or reading the help menu in Netbeans.