June 2001
Intermediate to advanced
888 pages
21h 1m
English
That debugging printout code is still not enough.
Use a debugger.
The JDK includes a command-line-based debugger, jdb, and there are any number of IDEs that include their own debugging tools. If you’ve focused on one IDE, learn to use the debugger that it provides. If you’re a command-line junkie like me, you may want to learn at least the basic operations of jdb.
Here is a buggy program. It has intentionally had bugs introduced so that you can see their effects in a debugger.
/** This program exhibits some bugs, so we can use a debugger */
public class Buggy {
static String name;
public static void main(String[] args) {
int n = name.length( ); // bug # 1
System.out.println(n);
name += "; The end."; // bug #2
System.out.println(name); // #3
}
}Here is a session using jdb to find these bugs:
ian> java Buggy Exception in thread "main" java.lang.NullPointerException at Buggy.main(Compiled Code) ian> jdb Buggy Initializing jdb... 0xb2:class(Buggy) > run run Buggy running ... main[1] Uncaught exception: java.lang.NullPointerException at Buggy.main(Buggy.java:6) at sun.tools.agent.MainThread.runMain(Native Method) at sun.tools.agent.MainThread.run(MainThread.java:49) main[1] list 2 public class Buggy { 3 static String name; 4 5 public static void main(String[] args) { 6 => int n = name.length( ); // bug # 1 7 8 System.out.println(n); 9 10 name += "; The end."; // bug #2 main[1] print Buggy.name Buggy.name = null main[1] help ** command list ...