Exercise 2 — Crash!
- At this point, clicking on the "Play" button will
probably result in an error message that looks something like
Exception in thread "Play" java.lang.IllegalArgumentException: Can't invoke onPlayButtonClick: java.lang.reflect.InvocationTargetException
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.
- 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.
- 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
onPlayButtonClick method of
BingoGame class. Open
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.
- 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
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
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.
Fix the error and write a short description
of what the problem was and how you solved it.