Debugging Applications
Debugging is the process of examining your application for errors. You debug by setting breakpoints and watches in your code and running it in the debugger. You can execute your code one line at a time and examine the state of your application in order to discover any problems. The IDE uses the Sun Microsystems JPDA debugger to debug your programs. When you start a debugging session, all of the relevant debugger windows appear automatically at the bottom of your screen. You can debug an entire project, any executable class, and any JUnit tests. The IDE also lets you debug applications that are running on a remote machine by attaching the debugger to the application process. When you run or debug web applications, JSP pages, or servlets, you can also use the HTTP Monitor to monitor data flow. The HTTP Monitor appears by default. The HTTP Monitor gathers data about HTTP requests that the servlet engine processes. For each HTTP request that the engine processes, the monitor records data about the incoming request, the data states maintained on the server, and the servlet context. You can view data, store data for future sessions, and replay and edit previous requests. For details on the HTTP Monitor, choose Help > Help Contents in the main menu. For free-form projects, you have to write an Ant target for the Debug Project command. You can also write targets to debug specific files and map these targets to the project's commands. In this section you will learn about: Basic DebuggingIn this section, we will use a simple example to demonstrate how to start a debugging session, step through your code manually, and monitor variables and method calls in the Debugging workspace. We will leave more advanced functions like setting breakpoints and watches for the following sections. Our example for this section is the Array Fill application. This application is very simple. It creates an array of sampleBeans, each one of which has two properties, firstName and lastName. It then assigns values to the properties of each bean and prints out the values. The first thing you want to do is run the application to see if it throws any exceptions. Create a Java application (Ctrl-Shift-N). Type Array Fill in the Project Name text box and specify a folder to house your project in the Project Location text box. Notice that the IDE uses the project name to fill the Create Main Class text box with arrayfill.main. This means that the main class main will be created in the package arrayfill. Change the name of the main class to arrayFill, so that the Create Main Class text box contains arrayfill.arrayFill. Click Finish. The new Java application is created and you can view it in the Projects window or Files window. Right-click the arrayfill package, choose New > Java Class, name the class sampleBean, and click Finish. Now insert the code that is attached to this page in your classes arrayFill.java and sampleBean.java. Make sure that arrayfill is the package declared in your Java classes. When you have inserted the code, right-click arrayFill.java and press Shift-F6 to execute it. The following output should appear in the Output window: java.lang.NullPointerException at arrayFill.loadNames(arrayFill.java:27) at arrayFill.main(arrayFill.java:34) Exception in thread "main" Java Result: 1 Starting a Debugging SessionWhen you start a debugging session in the IDE, the IDE compiles the files that you are debugging, runs them in debug mode, and displays debugger output in the Debugger windows. To start a debugging session, select the file that you want to debug and choose one of the following commands from the Run menu:
If more than one project is open in the IDE, make sure that Array Fill is the main project. Then press F7 to step into the main project's main class. If you did not set a main class, the IDE prompts you to set it. Then the IDE opens the file in the Source Editor, displays the Output window and Debugger windows, and stops just inside the main method. Debugger WindowsLet's take a minute to look at the Debugger windows. The Debugger windows automatically open whenever you start a debugging session and close when you finish the session. By default, the IDE opens three Debugger windows: the Local Variables window, Watches window, and Call Stack window. You can open other Debugger windows by choosing from the Window > Debugging menu. When you open a Debugger window during a debugging session, it closes automatically when you finish the session. If you open a Debugger window when no debugging session is open, it stays open until you close it manually. You can arrange Debugger windows by dragging them to the desired location. The following table lists the Debugger windows.
You can use the following commands in the Run menu to control how your code is executed in the debugger:
In our example, use the F7 key to step through the code one line at a time. The NullPointerException occurred in the loadNames call, so when you step to that call, watch the value of the names array in the Local Variables view. Each of the beans have a value of null. You can continue stepping through the loadNames method - the names beans are null throughout. The problem here is that while the line sampleBean[] myNames=new sampleBean[fnames.length]; initiates the array that holds the beans, it does not initiate the beans themselves. The individual beans have to be initiated in the loadNames method by adding the following code in line 28: names[i]=new sampleBean(); Working With BreakpointsMost applications are far too big to examine one line at a time. More likely, you set a breakpoint at the location where you think a problem is occurring and then run the application to that location. You can also set more specialized breakpoints, such as conditional breakpoints that only stop execution if the specified condition is true or breakpoints for certain threads or methods. In this section, we will use the arrayFill application from the last example, so you will have to recreate the bug by commenting out the code you added above. Setting a BreakpointIf you just want to set a simple line breakpoint, you can click the left margin of the desired line. A line breakpoint icon () appears in the margin. You can remove the line breakpoint by clicking it again. For more complex breakpoints, use the New Breakpoint (Ctrl-Shift-F8) command in the Run menu. The New Breakpoint dialog box lets you choose the type of breakpoint you want to create and set breakpoint options such as conditions for breaking or the information that the breakpoint prints to the Output window. Setting Conditions for a BreakpointConditional breakpoints only stop execution if a specified boolean expression is true. If you want to set a conditional breakpoint, open the New Breakpoint dialog box and enter an expression in the Condition field. For example, open arrayFill.java, set the insertion point in the loadNames method call in the main method, and press Ctrl-Shift-F8. In the dialog box, enter myNames=null in the Condition field and click OK. The conditional breakpoint icon () appears in the margin before the method call. Then press Alt-F5 to start debugging the application. The execution should break at the loadNames method call. Customizing the Output for a BreakpointIn the New Breakpoint dialog box, you can also specify what information is printed when a breakpoint is reached. Enter any message in the Print Text field at the bottom of the dialog box. You can use variables to refer to certain types of information you want displayed. Breakpoint TypesThe following table lists the different breakpoint types that are available.
Setting WatchesA watch enables you to track the changes in the value of a variable or expression during application execution. To set a watch, select the variable or expression you want to set a watch on in the Source Editor, then right-click and choose New Watch (Ctrl-Shit-F7). You can also create fixed watches in the Watches view. While a normal watch describes the content of a variable, a fixed watch describes the object that is currently assigned to the variable. To create a fixed watch, right-click any item in the Local Variables or Watches view and choose Create Fixed Watch. Previous - TOC
- Next |