Debugging Mini-Lab

Nathan Sprague
Kalamazoo College

 


Debugging

The goal of today's Mini-Lab is to get some practice with the BlueJ debugger by using it to track down some errors in an almost-finished Bingo project.

 

Exercise 1 — Stopping an Infinite Loop:

  1. Download and unzip the following unfinished Bingo project: BingoDebug.zip. Open the project in BlueJ and try to run it. You will find that nothing happens when the "Play" button is clicked: there is a bug in the program that causes the game to enter an infinite loop. We could try to track down the problem by carefully looking through the code for errors, or by systematically adding System.out.println calls. In this case it will be faster and easier to use the debugger.
  2. While your program is still stuck in the infinite loop, open the debugger by clicking on "Show Debugger" under the "View" menu of the main BlueJ window. You should see two entries in the "Threads" box at the top of the debugger window. If you select the "Play" thread, you will see a message that reads: "Thread is running. Threads must be stopped to view details." Click the "Halt" button at the bottom of the debugger window. The effect of that button is to force your Java program to stop at whatever line of code was being executed when the button was pressed. In this case, we are forcing the program to stop somewhere inside the infinite loop. At this point, you can resume the program by clicking "Continue", or you can step through the program one instruction at a time by using the "Step" and "Step Into" buttons. Click on the step button several times until you have a good idea of where in the code the infinite loop is occurring and why it is happening.
  3. Fix the infinite loop and write a short description of what the problem was and how you solved it. You will submit your answers at the end of the Mini-Lab. (Don't worry if the program still doesn't work after you fix this problem. There is more than one bug.)

 

Exercise 2 — Crash!

  1. At this point, clicking on the "Play" button will probably result in an error message that looks something like the following:
    Exception in thread "Play" java.lang.IllegalArgumentException: Can't invoke onPlayButtonClick: java.lang.reflect.InvocationTargetException
    	at edu.kzoo.grid.gui.GeneratedButtonList$GeneratedThreadedControlButton.act(GeneratedButtonList.java:383)
    	at edu.kzoo.grid.gui.ThreadedControlButton$1.run(ThreadedControlButton.java:67)
    
    Sometimes these error messages contain useful information. Not this time. There is no indication here of what is going wrong, or where in the code it is happening. Once again, we will use the debugger to track down the problem.
  2. As a first step, click on "Tools → Preferences..." in the main BlueJ window and check the box next to "Display line number". Now when you open up any source file you will see line numbers along the left side of the editor window.
  3. We don't know where exactly the program is crashing, but we do know that it only happens after the "Play" button is clicked. The "Play" button interacts with our code by calling the onPlayButtonClick method of the BingoGame class. Open BingoGame and left-click on the number to the left of the first instruction in the onPlayButtonClick method. You should see a small stop sign appear. That stop sign is a break point. The program will stop whenever it reaches that point. Try running the program to confirm that the break point is working as expected.
  4. You should now be able to narrow down the location of the problem by stepping through the program one instruction at a time. Start out by using the "Step" button to find which line in onPlayButtonClick is causing the problem. (Note that the program won't obviously crash when the problem instruction is executed; it will just fail to move on to the next logical instruction.) Once you know which line in onPlayButtonClick is causing the problem, you can restart the program, use the "Step" button to get back to that line, and then "Step Into" that instruction to continue narrowing down the location of the problem. Continue this process until you know exactly what line in is causing the program to crash.
  5. Fix the error and write a short description of what the problem was and how you solved it.

 

Exercise 3 — Winning the Game:

  1. At this point the Bingo program runs without crashing. Unfortunately, the game always finishes after the first number is called. There is a bug somewhere in the logic that determines if the card is a winner. Find the error by thinking about where in the code the problem might occur, and by using the debugger to narrow down the possibilities.
  2. Fix the error. Write a short description of what the problem was and how you solved it.

 

Hand in the problem descriptions you developed for the three exercises above.