1. Technology

Simple Debugging With NetBeans

By

Businessman working on a laptop
Neustockimages/E+/Getty Images

In getting started with NetBeans the end result was a simple "Hello NetBeans World" program. Writing the program only needed the input of one line of Java code and as long as the line was typed correctly the program should have compiled without any problems. Of course, this doesn't happen every time a programmer writes a program so it's time to look at what happens when there is an error.

Debugging a Java Program

Debugging is the process of finding and fixing errors within a program. The easiest error types to find are the ones that stop a program from compiling. These errors will have caused the compiler to complain because the Java syntax is incorrect and doesn't make sense. The result is similar to getting the syntax of an English sentence wrong and saying something like "Want a fish plate please chips of and I"!

Let's take the "Hello NetBeans World!" program and break it to see what happens. For example, if we misspell println :

public class Main {

  public static void main(String[] args) {

    System.out.prontln("Hello NetBeans World!");

  }
}

The first thing you should notice is that as soon as you change the "i" to an "o" a squiggly red line suddenly appears under the offending part of the Java line - "prontln". NetBeans is happy to point out compile-time errors as soon as you type them.

To find out exactly what the error is, move the cursor over the red squiggly line. A pop-up message will appear showing the message the Java compiler will generate for the misspelling:

cannot find symbol
  symbol: method prontln(jav.lang.String)
  location: class java.io.printStream

The cannot find symbol error message is essentially saying that it doesn't know what "prontln" is referring to. Changing the "o" back to and "i" will see the red squiggly line disappear and the Java code will now compile successfully.

Debugging will become second nature the more Java programs you write. Remember to look out for the red squiggly lines after you have finished writing your Java code. They will highlight the compile-time errors and make it easier for you to get your Java program running.

  1. About.com
  2. Technology
  3. Java
  4. Tools and Utilities
  5. IDEs
  6. NetBeans
  7. Simple Debugging With NetBeans

©2014 About.com. All rights reserved.