Debugging a class

It is possible to set breakpoints and watches in NetBeans and, on execution, check what the value of a given variable or object is.

NetBeans debugger also lets the developer follow method calls and execute code one line at a time giving a fine-grained visualization on how the code is running.

This is one of the features where NetBeans shines in comparison to other IDEs so without further ado, let's dive in. NetBeans has already included many of the plugins, performance tools, and servers that are used for easing the process of setup and debugging.

Getting ready

We will be using a Java Application Project for this example. Since we are just showcasing the capabilities of the debugger too, we won't be using anything complicated; a normal Java Application Project will suffice. If you're unsure on how to create one, please check the recipe Creating a Java Project using Wizard.

We are also going to need a Java class. If the Creating a Java Project using Wizard recipe was used, then a main class is already generated and that one can be used in our example. If a project already exists, then it is possible to follow the Create a class recipe in order to get a clean class for the work to proceed. The automatically generated class will be our main class, so the class name is Main.java.

If the Creating a Java Project using Wizard recipe is followed, the project name used for this recipe would be HowToDebugApp and all of the default settings present on the wizards should be left untouched.

How to do it...

We will need another class to demonstrate how to setup breakpoints in other objects.

  1. Right-click on Source Packages, under the HowToDebugApp project, select New and Java Class....
  2. On the New Java Class window, type Person under Class Name.
  3. On package selection, click on the dropdown and select howtodebugapp.
  4. Click Finish.

The Person.java file will show up on the Java Editor.

Inside the Person class declaration, write:

int age;
String name;

Now lets refactor our class by encapsulating the fields:

  1. Right-click on the Person class inside the Java Editor.
  2. Select Refactor and then click on Encapsulate Fields....
  3. Then click on Select All and Refactor. The getters and setters will be added to Person.java and the fields will have the visibility modifiers set to private.
  4. Open Main.java. If Main.java is not open in the Java Editor, double-click it.

Inside the main method, enter the following:

Person person1 = new Person();
Person person2 = new Person();
person1.setName("Rhawi");
person1.setAge(28);
person2.setName(null);
person2.setAge(32);

It is not good practice to set variables with null values but, for the purpose of this example, we will do it this one time.

A watch is exactly what the name says, it "watches" a specific variable for the entire lifetime of the application and displays its value in the variables view.

To add a watch, simply:

  1. Select person1 variable.
  2. There are two ways to add a watch: On the top bar, click on Debug and then New Watch... or press Control+Shift+F7 and click OK. person1 will be added to the Variables view.
How to do it...

For our first breakpoint, click on the left-side bar, where line numbers are placed, specifically on the line shown below:

person2.setName(null);

A breakpoint will be added to the side bar and a long pinkish line will specify the breakpoint location.

How to do it...

Then add another breakpoint, but this time to Person.java, where the line is:

this.age = age;
How to do it...

Finally, let's debug our example. Open Main.java and press Control+Shift+F5.

How it works...

Upon the debug mode execution, NetBeans stops the execution at the first breakpoint.

The breakpoint tells the IDE to temporarily halt the program and let the developer examine the contents of variables and watches at that point.

In our example, NetBeans will look like this:

How it works...

It is possible to see our watch, shown with a blue diamond icon, and also the normal variables, shown with a green lozenge. It is possible to expand the watch or variables to check the contents of the object on runtime.

The execution point is highlighted in green. To jump one line of code in the execution, press F8. To continue press F5; the IDE will resume the debugging process until another breakpoint is found, at which point the execution will once again stop, so that the developer can have another chance to examine the execution. In our example, the execution will continue until the Person's set method for age is reached. To continue with the execution, just press F5 one more time and the program execution will continue until the end, since we do not have any other breakpoints.

To remove a breakpoint, simply click on it and it will disappear.

There's more...

What if I want a breakpoint when my variable reaches a certain value? Watches are too complicated, is there something easier? Does NetBeans debug other types of applications?

Conditional breakpoints

It is possible to create breakpoints with conditions.

Just right-click on the breakpoint, select Breakpoint, and then Properties. A Breakpoint Properties window will show up, with a section where the conditions can be specified.

Other ways to check variable content

It is also possible to check the contents of a variable without needing to watch the Variables view. When the execution stops, just place the mouse pointer over the variable. A tooltip will be shown with the variable result.

This is very fast and useful especially when you first start debugging, and don't know what watches are needed yet.

Different kinds of debuggable applications

In this recipe, we used a Java Desktop application as a base for our example. But debugging can also be used with different applications, such as Web Apps and Mobile Apps, which are also written in different programming languages.

..................Content has been hidden....................

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