Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Use the Java Debugger to Determine the Causes of Errors : Page 3

Although the Java Virtual Machine frees programmers from many low-level tasks like garbage collection, it can't protect them from mistakes in design and logic. This article explores the Java Debugger, which you can use to trace through a running program and determine the causes of any errors.

Breakpoints: The Most Useful Debugger Feature
The most common debugging technique is placing a breakpoint in a source line where you want to investigate either the state of variables or the visual appearance of the output screen. You might use this technique when you have an if statement that never seems to execute, for example. You might want to see the contents of the variables that are used in the if statement's condition and find out to what they evaluate.

In fact, a breakpoint is the most useful feature in a debugger like JDB. It causes a running program to pause execution at the point you specify. You can see what the user interface looks like or examine any variables before resuming execution again. This feature is tremendously useful for understanding exactly what is happening within a program.

For example, say you want to know how much money is in each of your students' bank accounts before they buy any pizza or pay their rent. Looking at your program code, you see the following series of lines:

Student2.StudentNumber = 30; Student2.Name = "Bill"; Student2.depositMoney (300); Student1.buyPizza (); Student2.buyPizza (); Student1.payRent ();

To set a breakpoint, you first need to start a JDB session with the class file you're investigating. Then instruct JDB to set a breakpoint at a specific line of program code. The breakpoint must be placed on a line of actual program code, not a blank line or a line with just a comment. The latter will result in an error.

Student1.buyPizza() is first called on line 59. Although you don't use line numbers in Java, you need to refer to specific line numbers to tell JDB where to break (pause) execution. Thus, it is helpful if your text editor can tell you the number of a particular line in the file. All modern IDEs have this facility. UNIX and Linux users using vi can press CTRL+G to see the number of the line on which the cursor is resting.

The JDB command to set a breakpoint on line 59 is as follows:

stop at students:59

JDB responds as follows:

Deferring breakpoint students:59. It will be set after the class is loaded.

JDB is telling you that it is going to set a breakpoint where you specified it as soon as it loads the class. It hasn't loaded the class yet because you haven't actually begun execution. Start your program with the JDB run command and JDB will execute each line until it reaches your breakpoint.

As soon as the program reaches the breakpoint, it pauses. You then can enter JDB commands again, giving you a chance to inspect variables or other data elements and check that any output is what you expect (see Figure 2).

Figure 2: Running Your Program in the Debugger

To see just where the program has stopped, you can use the list command. It shows a few lines of source code, with an arrow indicating where execution stopped.

To inspect the value of the students' bank accounts, use the following commands:

print Student1.getBalance () print Student2.getBalance ()

Try Other JDB Commands
Experiment with more of the commands listed in Table 1. For instance, use next to step line by line through the program. Use print Student1.getBalance () again after each next command to see how student 1's cash balance changes after method calls. The locals command displays the value of all local variables simultaneously.

Try setting a breakpoint inside one of the methods in the student class. You can break inside the payRent() method using the command stop in student:payRent. Now use the where command to see the call stack of your program.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date