|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
Stepping Through a ProjectBy now youve probably selected Run and had the LearnToDebug application execute in the debugger. If you havent, stop reading now and do it. This part is not too exciting, but were ready now to get into the nitty-gritty of navigating through projects while in a debug session. Step IntoThe first command well try is the Step Into command. If Visual J++ still has the default shortcut keys, you can press F11. Its much easier than selecting Debug, Step Into. Make sure that the program is not running, and then select Step Into (or press F11). Youll 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 youre 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 youll find yourself at the Form1 constructor. If your program has anything thats initialized, youll be able to step through that code here. Because were working with a program thats 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 ideathe Step Into command steps through your program one line of source code at a time. EndLets 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 OverThe default shortcut key for the Step Over command is F10. Lets say you dont 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 isnt 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 didnt have to walk through (or should I say drudge through) the code in the initForm() method.
Step OutOkay, you made a mistake and accidentally used Step Into one too many times. Youre 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 youre in and exits. The default shortcut key for this command is Shift+F11. Run To CursorTheres one last navigation command that will make your debugging easier: the Run To Cursor command. Lets say theres one line toward the initForm() method thats doing something strange. You want to run the program until you get to that line and see whats 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 WindowsWhen you debug programs, youre 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. Thats why knowing whats 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, youll 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";
|
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. |