Level 2 – Debugger Basics (≈ 12 minutes)
Concepts: breakpoints, stepping over/into/out, variable inspection, tracking changing state, logic bugs.
We will reuse the Level 1 code, but now solve the bugs with the debugger instead of just reading messages.
1. Confirm your code
You should still have:
BuggyCalculator.javaBuggyLoop.javaMain.java
If you already fixed the code in Level 1, temporarily undo those fixes (or paste the original versions from Level 1) so that:
dividecan still divide by zero.sumFirstThreestill loops toi <= 3.countUpTostill usesfor (int i = 1; i < n; i++).
We want the bugs back so we can practice debugging them.
2. Set your first breakpoint
- Open
Main.java. - Click in the gutter on the line:
int result = BuggyCalculator.divide(10, 0);
- You should see a red dot (breakpoint) next to that line.
Start the debugger:
- Click Debug.
- When execution pauses, the breakpoint line will be highlighted.
3. Step over and inspect variables
When paused at the breakpoint:
- Look at the Variables/Locals panel:
- You should see
argsand any local variables.
- You should see
- Click Step Over:
- This executes the
dividecall and moves to the next line (or throws an exception).
- This executes the
If an exception is thrown:
- Notice how the debugger shows the line that crashed.
- Compare to the stack trace from Level 1.
Repeat, but this time:
- Set a breakpoint inside
BuggyCalculator.divide, on the linereturn a / b;. - Start Debug again.
- The debugger will stop inside
divide.
Use the debugger to answer:
- What are the values of
aandbright before the division? - What happens if
bis zero?
4. Step into another method
Practice Step Into and Step Out with sumFirstThree:
- Set a breakpoint in
Main.mainon:int sum = BuggyCalculator.sumFirstThree(nums);
- Start Debug.
- When paused on this line, click Step Into.
You should now be inside BuggyCalculator.sumFirstThree.
While stepping through the loop:
- Watch
i,total, andnumbers.lengthin the Variables panel. - Use Step Over to run each iteration.
- Notice when
ibecomes an invalid index.
Optional: add a Watch expression (if available) for numbers[i].
When you understand why it crashes:
- Click Stop to end the debugging session.
- Fix
sumFirstThreeso it:- Does not crash when the array is short.
- Still correctly sums up to the first 3 items when they exist.
5. Debugging a logic bug with stepping
Now fix BuggyLoop.countUpTo using the debugger, not just by staring at the code.
- Set a breakpoint in
Mainon:int count = BuggyLoop.countUpTo(5);
- Click Debug.
- When paused, click Step Into to enter
countUpTo. - Use Step Over to step through each loop iteration.
Observe:
- How many times does the loop run?
- What are the values of
iandcountat each step?
Use this observation to:
- Decide what the loop should do.
- Update the loop condition so
countUpTo(5)returns the expected value.
Re‑run with the debugger to confirm the fix.
6. Compare debugger vs print debugging
Briefly discuss or think about:
- When was the debugger faster or clearer than
System.out.println? - When might print debugging still be useful?
- How did stepping into
sumFirstThreeandcountUpTohelp you see the bug more clearly?
You are now ready to debug across multiple files and classes in Level 3.