BlueJ Debugger

Sometimes you have a loop that will never end, or unexpected results. Rather than quit BlueJ altogether, you can actually kill your method in its tracks or even inspect the varibles at every step of the loop.

Show Debugger

From the Veiw Menu, select Show Debugger (Cmd-D or Ctl-D will do the trick as well)
Windoze:


OSX:

How to Halt your program

First, let us make a problem.
  1. Make a New Project and call it "Bad News"

  2. Make a new Class and call it "Forever"

  3. Double-Click your new Class called Forever, and make a new method by selecting from the Edit Menu, Insert Method (Ctr-M or Cmd-M works too)

  4. Now edit the Forever class into this nasty bit of code, and Compile it:

    If you are lazy (or in a hurry), you can forget all that and simply paste this:
       
    public class Forever
    {
        public static void main (String args[])
        {
            System.out.println("Loopy is a loopy thing");
            System.out.println( "y=" + loopy(23) );
        }
    
       
        public static int loopy(int y)
        {
            for (int i=0; i <10 ; i++) 
            {
                y=i;
                i=3;
            }
            return y;
        }
        
    }
    
    

    If you like to see the line numbers you can change the Preferences (from the Tools Menu of BlueJ -not the edit window- to look like this:


  5. Though this Compiles, there is a nasty bug. Make sure have selected "Show Debugger" be the next step.
  6. Go ahead and execute the main method, and it will run forever. The only way to stop it is to kill Blue-J (not good) or to press the Halt or Terminate buttons on the Debugger (You DID veiw the Debugger, didn't you? (Windows XP has a glitch (feature?)in the way it excutes threads, so the Debugger has to have priority in order to work properly to kill your method's thread. This is not an issue in Linux, OSX or hopfully in Vista). If you are running XP and forget to show Debugger before you execute your method, you will have to Quit BlueJ, Restart BlueJ, Show the Debugger, THEN call your method!)

  7. You probably see something like this:

  8. Look at the Debugger Window. Click on the main (running) method in the Debugger window:

  9. If you click the Terminate button it simply kills the execution.
  10. If you click the Halt button you get a little more insight on what is going on (Can you find where it shows you the values of your local variables i and y?:

  11. Press the Step button, and it will show you where in the source code you are (look for the black arrow). Notice that the Debug window shows you that y=4 and i=4. Also notice that it shows you a list of active methods on the left. Forever.main is on the bottom, as it was called first, then Forever.loopy was called next, so it was put on top of the main method call.

  12. Keep pressing the step button and watch the arrow in the source code and the values of y and i. Can you see the problem? i will never get to 10 to end the for loop, since inside the for loop, it keeps being reset back to 3. If you get rid of that line, then the loop will finish, and all the methods will be able to execute properly.

Testing a Method without an Application nor an Applet

Since this is a static method, if you right-click the Forever Class, you can execute this method without actually making an Application or Applet (This is a Blue-J thing, not a Java thing).



Make sure your Show Debugger first if you want to be able to terminiate or Halt an endless loop (Windows XP has a glitch in the way it excutes threads, so the Debugger has to have priority in order to work properly to kill your method's thread. This is not an issue in Linux, OSX or hopfully in Vista). If you are running XP and forget to show Debugger before you execute your method, you will have to Quit BlueJ, Restart BlueJ, Show the Debugger, THEN call your method!

Setting Your Own Break Points

You can add your own "break points" in the code to have the Debugger automatically stop the execution when it gets to that point in the code.
  1. To demonstrate, make a new method:
     public static String fromAnyToDec(String theNumber, int base)
        {
            int decNum=0;
            int last=theNumber.length()-1;
            for (int i=0; i<= last; i++)
            {   
                int digit=Integer.valueOf(theNumber.charAt(i));
                decNum+=digit*Math.pow(base, last-i);
            }
            
            return String.valueOf(decNum);
        }
     
  2. Comple and Right-Click the class and execute the fromAnyToDec method

  3. Lets test it out by seeing if it can convert "111" in base 2 into "7" in Base 10:

  4. Seems to be a problem here:

  5. We will now set a break point to watch what is happening one step at a time. Go to the source code and click in the left gutter next to the last line of the loop (See the STOP sign on my line 26?):

  6. Right -Click again and the Debugger Window will pop up when it gets to the place where you told it to stop. Look at the values of the variables.

  7. Press the step button a few times, and each time keep an eye on the values of the variables and the black arrow in the code that shows you what line you are about to execute next.

  8. The problem is subtle. See how digit= 49? Shouldn't it be 1? You thought that the line
    int digit=Integer.valueOf(theNumber.charAt(i) );

    would get the ith digit from theNumber, and convert it into an integer and put it into digit. The Debuger shows digit has a bogus value. That has to do with the fact that charAt returns a simple data type char rather than a String, and the valueOf method of the Integer class was expecting a String.

  9. There is more than one way to fix this, but one way is to replace this line with one that uses substring() in place of charAt, since substring returns a String. Go ahead and fix the line so it looks like this instead:
    int digit=Integer.valueOf(theNumber.substring(i, i+1));

  10. Now compile and execute the method again and all should be well!

Tip: A good place to put break points are at the beginning of a loop.