C H A P T E R  5

Debugging MIDlets and MIDlet Suites

This chapter describes how to debug MIDlets and MIDlet suites in the Java Studio Mobility IDE.

While this tutorial does not describe all the features of the IDE, the tutorial takes you through a short debugging session using the Currency Converter application you installed in Chapter 2. The steps are described in the following sections:

MIDP Debugging in Java Studio Mobility

If you have ever written a program before, you know that programs do not always perform in the way you expect them too. A program might display an incorrect value, go the wrong screen or page, or fail to display the information you expect to see. The process of finding why these mistakes are occurring is called debugging.

In the debugging process, you use breakpoints to stop the execution of the program and to examine its state at the location it has stopped. For example, if the currency conversion is displaying the wrong value, you can set a breakpoint in the code that does the conversion and examine the values of variables used in the computation. If your program is going to the wrong page when a soft button is pressed, you can set a breakpoint in the commandAction() method and single-step through it to see why it is displaying the wrong page.

This debugging example, designed to show just a few of the features of the Java Studio Mobility debugging environment, assumes that you want to examine the values used in the conversion. This example uses the Currency Converter application you installed in Chapter 2.

In this debugging session, you will:

Setting a Breakpoint

The first step in debugging is to set a breakpoint in the code where you first want to examine the execution of the program. Therefore, you want to set a breakpoint in the conversion code. At this point, however, you are not yet familiar with the code, and don't know where this point is. This is typical when debugging code you are not familiar with so you want to set a breakpoint in the code that initiates the conversion. Conversions are done as each number is entered so you want to set a breakpoint in the itemStateChanged() method in the Converter class. To set a breakpoint:

1. Expand the converter folder so its contents, the three java source files of the Currency Converter MIDlet, are displayed.

2. Double-click on ConverterMIDlet.

The ConverterMIDlet.java source file is shown in the Source Editor window.

3. Scroll down in the MIDlet to the convert() method, the last method in the file.

4. Set a breakpoint by clicking in the left margin of the code line:

return (frval * rates[fridx][toidx]) / 1000000;

The line is highlighted in pink, and is set as a breakpoint.

Note - Clicking the square on the left a second time will remove, or unset, the breakpoint setting.

Screenshot of Explorer window with highlighted code.  

Running a Debugging Session

To make use of the breakpoint you just set, you need to run the MIDlet under the control of the debugger. If you execute the MIDlet using the Execute command, the IDE will ignore the breakpoint while executing the program.

1. Start the Debugger by selecting the converter MIDlet Suite in the Filesystems window and choosing Debug right arrowStart Session right arrow Run in Debugger.

The program runs, opening a device emulator. Notice that a Debugging window opens in the lower right corner. This is called the Debugging workspace. You can change the view using the tabs at the top of the window.

Note - If the emulator does not run as expected, make sure you have selected the converter MIDlet suite and not the Converter MIDlet.

You've now established your debugging environment, so you are ready to debug the application. To find the method you're looking for, you now need to cause a conversion to occur. This is done by interacting with the MIDlet in the device emulator.

 [ D ] 

2. In the device emulator, click the Launch button to start the Currency Converter application.

The emulator screen displays the three currency fields: US, Yen and Euro.

3. Click in the device emulator, click the "5" button.

The numeral "5" appears in the US field. The IDE shows that the application has been activated, and the program runs until it hits the breakpoint at the convert() method. Notice that the line is now green, and the icon to the left has an arrow.

Now the debugger has control of the application, and you can begin to execute the application one source line at a time, or step through the application, until you find the line that will do the actual conversion.

 [ D ] 

4. Choose Debugright arrowStep Over from the Main Menu to execute a line.

The debugger moves to the next line of code where the conversion occurs.

The function key shortcut for stepping over is the <F8> key. If you are single-stepping through many lines of code, it is often easier to use the shortcut.

5. Use the <F8> key to continue stepping through the code until the green line is on the source code line:

((TextField)get(i)).setString(String.valueOf(midlet.convert(value, from, to)));

This is a complex line with several method calls. The midlet.convert() call is the one that will do the actual conversion, so that is the call you are interested in. You need to step into this line, because midlet.convert() is actually the second method to be executed.

Screenshot of the Source Editor window with text highlighted in green, as described. 

6. Choose Debugright arrowStep Into or press the <F7> key.

A dialog appears that informs you that the source of the get() method has not been found in the mounted filesystems. That is because the method invoked is a J2ME method, and is not part of the Currency Converter MIDlet.

Screenshot of dialog. Radio buttons are Stop, Step Out, and Stop at the first line with source. Checkbox is Do not show this dialog next time. Button is OK. 

7. Ensure that the Step Out radio button is selected and click OK.

8. To continue, choose Debugright arrowStep into, or press the <F7> key.

This takes you past the get() method and to the midlet.convert() method.

9. Continue pressing the <F7> key.

If the dialog appears again, select OK and continue to step into the program until you get to the following line:

return (frval * rates[fridx][toidx]) / 1000000;

You have now found the line that contains the variables used for conversion. You can examine the variables in the line using a "variable balloon."

10. Place the mouse cursor over the variable frval.

A balloon caption appears indicating that the current value of frval is 5, the number you typed in when you began the debugging session.

Screenshot of Source Editor window, with return line highlighted and balloon help indicating the value, frval=5. 

11. Place the mouse cursor over the values fridx and toidx to check their values.

In this way, you can verify that the values are correct throughout your code.

12. To end the debugging session, choose Debugright arrowFinish from the main menu.

This concludes this debugging session.