home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Stepping Through a Project

By now you’ve probably selected Run and had the LearnToDebug application execute in the debugger. If you haven’t, stop reading now and do it. This part is not too exciting, but we’re ready now to get into the nitty-gritty of navigating through projects while in a debug session.

Step Into

The first command we’ll try is the Step Into command. If Visual J++ still has the default shortcut keys, you can press F11. It’s much easier than selecting Debug, Step Into.

Make sure that the program is not running, and then select Step Into (or press F11). You’ll come to a line of source code in the LearnToDebug program that looks like the following:

Application.run(new Form1());

All of our applications will start with this line. The only exception will be if the form is named something other than Form1. For example, if you’re debugging a program other than the LearnToDebug program, your form might be named MyGreatForm, in which case the first line would look like the following:

Application.run(new MyGreatForm());

Employ the Step Into command again and you’ll find yourself at the Form1 constructor. If your program has anything that’s initialized, you’ll be able to step through that code here. Because we’re working with a program that’s been created by Visual J++, there is nothing here except a call to the initForm() method.

Your next use of Step Into brings you to the place that creates a new Container object. Pressing F11 again gets you into the initForm() method. Each time you press F11, you step through another line of code in the initForm() method until that method is completely executed. Then you return to the end of the Form1 constructor. You get the idea—the Step Into command steps through your program one line of source code at a time.

End

Let’s practice the End command. Select Debug, End or press Shift+F5. The application will no longer be in debug mode, and Visual J++ will return to its normal state. The only difference might be this: when the debugger stops at a line of source code, the source-code editor stays at that location. So if your source code showed the top of the Form1 source-code module when you started debugging and you stop the debugger in the initForm() method, the source code you see when the debugger stops is the code in the initForm() method.

Step Over

The default shortcut key for the Step Over command is F10.

Let’s say you don’t want to fool around with stepping through the initForm() method. You know that everything in it works fine, and there might be a hundred lines of code by the time your application is developed. For that reason, the Step Over command was invented. You can step over an entire method (and methods which that method calls).

Make sure that the debugger isn’t running. Execute a Step Into command, and you should see the first line of code. Use the Step Into command until the cursor is on the call to initForm() inside of the Form1 constructor. The following source code shows you where to stop:

public Form1()
{
    // Required for Visual J++ Form Designer support
    initForm(); // Stop when the debug cursor is on this line.

    // TODO: Add any constructor code after initForm call
}

Instead of using Step Into, which you know will step you right into the initForm() method, use Step Over (you can use the F10 shortcut). Voilá! All the code in the initForm() method was executed, but you didn’t have to walk through (or should I say drudge through) the code in the initForm() method.


Tip:  Use the Step Over command whenever possible during your debug sessions. If you know that a method has no problems, there’s no reason to waste time stepping through its code.

Step Out

Okay, you made a mistake and accidentally used Step Into one too many times. You’re now on the first line of the initForm() method. All is not lost. Visual J++ has a very nice command called Step Out. It proceeds to the end of whatever method you’re in and exits. The default shortcut key for this command is Shift+F11.

Run To Cursor

There’s one last navigation command that will make your debugging easier: the Run To Cursor command. Let’s say there’s one line toward the initForm() method that’s doing something strange. You want to run the program until you get to that line and see what’s going on. This is easy. In your source code, click on the line at which you want the debugger to stop. Use the Run To Cursor command by selecting it from the Debug menu or pressing the default Ctrl+F10 key combination. Your program will execute and stop at the point where the cursor is.

Debug Windows

When you debug programs, you’re going to rely on the tools that help you view the contents of variables. The values that are stored in variables determine the results of calculations and the flow of the program. That’s why knowing what’s in variables is at the crux of debugging.

This section shows you how to use the Visual J++ debug windows. Knowing how to use them properly will help you get the most out of them. And this will make your debugging sessions far more productive.

Before we get started, you’ll need to add some variable declarations and assignments to the LearnToDebug project. At the top of the Form1 class, add the following code and rebuild the application:

public class Form1 extends Form
{

    int m_nHorizontal = 50;
    int m_nVertical = 75;
    boolean m_bButton = false;
    double m_dValue = 4.5;
    String m_strText = "This is text";


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.