Debugging in Netbeans

Debugging Java programs in Netbeans is fun and easier (and faster) than opening a web page in Internet Explorer. If you start programming for a living you will quickly realize that no matter how smart you are, your programs are smarter than you. Your programs will suddenly start behaving erratically and out of their own sweet will. Worst of all, they tend to do that at worst possible moments, e.g. during a much publicized and well rehearsed demo. Debugging is the arsenal in your toolbox to tame those bots. Netbeans, the leading Java IDE makes the whole process cakewalk.

What is Debugging?

Debugging is the act of analysing source code to determine the cause of an unexpected or unwanted behaviour.

Debugging a Java program typically involves connecting to a local or remote Java process, setting  a breakpoint to temporarily halt the execution of a program and watching variables and expressions to troubleshoot mysterious behaviours.

Debugging Java is easy because of the Java Platform Debugger Architecture. Netbeans makes it easier by using JPDA to provide an easy to use interface for debugging.

JPDA: The Unsung Tsar of Java Specs

If you have ever programmed in one of the more traditional programming languages such as C or Fortran, you will immediately realize the tremendous convenience in debugging Java. Debugging Java is so easy primarily because of Java Platform Debugger Architecture or JPDA for short.

JPDA is a series of three specifications and includes –

  1. JVM TI – stands for Java Virtual Machine Tool Interface. This is the heart of JPDA and can be thought of as debugging support inside JVM. Since JVMs implement JVM TI it is possible to debug JVM. You would appreciate that this is a marked improvement over debugging an executable by peeking into processes using nasty sequence of forks and ptraces.. The JVM TI makes the information available without child process jugglery.
  2. JDWP – Java Debug Wire Protocol. Since Java is the preferred platform for for multi tier transactional web software, JDWP specifies a format (but not the method) to transport debug data over network. This makes debugging a remote Java process identical to debugging on your local desktop. Since JDWP is silent on transport methods, it leaves room for the implementation to decide which transport protocol to use.
  3. JDI – stands for Java Debug Interface. This is a Java layer that is meant for debugger developers who don’t want to deal with JDWP and JVM TI themselves.

As we will seen in the next few sections, Netbeans makes debugging a breeze and you can afford to be oblivious of JPDA and its constituents.

Binary Search: Bugs Bunny of Computer Science

Prof. Donald Knuth, the most eminent computer scientist to ever walk this earth is reported to have said

Although the basic idea of binary search is comparatively straightforward, the details can be surprisingly tricky … — Donald Knuth

Let’s try it ourselves.

The Basic Idea of Binary Search

A binary search works on dividing the search space repeatedly into halves. Dividing your search spaces by a factor of 2 has exponential benefits and therefore leads to a logarithmic time algorithm for searching.  One of the basic requirements for binary search to work is that the elements must be presented in a sorted form.

Here is a basic recursive implementation of binary search

and the following is an iterative version of binary search –

Bugs and Traps in Binary Search

There are multiple things that can (and will when the worst moment arrives) go wrong with this binary search –

  1. There is no check for array boundaries
  2. There is an off-by-one problem in condition check statement
  3. There are no checks for validity of method arguments
  4. -1 is abused to indicate unsuccessful search
  5. The code does not seem to care about duplicates. Should we return the first value encountered or the last value?
  6. And, of course, the classic binary search bug where
    1mid = (high + low) / 2;

can overflow and must be replaced by mid = low + (high – low)/2;

Let’s just focus on one of these issues and debug it in Netbeans.

Debugging in Netbeans

Assuming you have already followed along the Netbeans 101 lesson, here are the broad steps for debugging in Netbeans –

  1. Create a Netbeans project or import your Eclipse project into Netbeans
  2. Write your source code
  3. Set a breakpoint in your code
  4. Start the local debugger by either using the keyboard shortcuts of Ctrl + F5 or Ctrl + Shift + F5 or using the Debug menu or selecting ‘Debug‘ on right clicking the project in the Projects window
  5. Analyse the values of variables. You can also watch an expression.
  6. Press F8 to let the control flow normally, F7 to go into a method, Ctrl + F7 to come out of a block, F5 to let the control flow till next breakpoint, F4 to run to cursor and Shift + F5 to end the show.

Let’s go over the steps using our binary search as an example.

1. Create a Netbeans Project

Go to File -> New Project

Select Java Application and give a suitable name to your project –

2. Enter Your Source Code

Enter the following iterative version of binary search into your main file –

3. Set a Breakpoint

First run your code. We are invoking our binary search method from the main method, run the file by pressing F6. You should see the following output –

Now add the following code to your main method –

You should now see an exception similar to the one below –

Time to begin debugging by setting a breakpoint. To set a breakpoint you need to have at least a slight idea of where the problem might be. In the current scenario our stacktrace gives a good indication of the problematic line number. The stacktrace is complaining about exception originating at line number 54, so let’s set breakpoint there.

Setting a breakpoint is quite easy. Just double click on the line number to the left side of your source code editor. A red square along with line highlighting indicates that a breakpoint has been set. You can set as many breakpoints as you like before commencing debugging.

4. Debugging in Netbeans

Having set the breakpoint, initiate debugging by pressing Ctrl + F5. The code till your breakpoint is executed and the execution temporarily stops at the breakpoint. This gives you a chance to look at the values of variables.

In our case the breakpoint is inside a while loop. You can press F8 to examine the effects of execution of individual statements or press F5 to see the effects for an entire iteration at one go.

After few iterations of the while loop you should see the following state of variables –

Note that the value of mid is 5 and this is the root cause of the entire problem. A visual examination reveals that the largest index of our array is 4, but we are trying to access the index #5. The problem originated with the statement

This needs to be fixed as

Now re-run the code by pressing F6. You should be able to run your binary search without issues, at least for the moment. Through debugging we have fixed an off-by-one bug. There are more issues in that small piece of code, happy hunting and happy debugging in Netbeans.

Further Reading

Interested in Searching and Sorting Algorithms? Look no further than the The Art of Computer Programming by Donald E. Knuth.

Leave a comment

Your email address will not be published. Required fields are marked *