Basic Troubleshooting Lesson

Becoming a Code Detective

Troubleshooting is the systematic process of diagnosing and solving problems in your code. It's what you do when your program doesn't compile, crashes, or produces the wrong output. Adopting a patient, logical mindset is the key to becoming an effective troubleshooter.

The Troubleshooting Mindset

Before diving into specific techniques, it's important to approach problems with the right attitude. Every programmer, from beginner to expert, spends a significant amount of time debugging.

🧘 Be Patient

Don't get frustrated. Finding and fixing bugs is a normal and expected part of programming. Every error is a learning opportunity.

🔬 Be Systematic

Don't just change things randomly and hope for the best. Follow a logical process of elimination to narrow down the cause of the problem.

🤔 Assume It's Your Code

While external factors can cause issues, the bug is almost always in the code you've written. This mindset helps you focus your search.

A Step-by-Step Guide to Troubleshooting

When your program isn't working, follow these steps to systematically find and fix the issue.

Step 1: Read the Error Message Carefully!

This is your most valuable clue. The error message will tell you what went wrong and where it happened. Pay close attention to the error type (e.g., `NullPointerException`) and the file and line number it points to.

Step 2: Check Your Recent Changes

The golden rule of debugging: "What did I just change?" More often than not, the bug you're seeing was introduced in the last few lines of code you wrote or modified. Review your recent work first.

Step 3: Use Print Statements (`System.out.println()`)

This is the most fundamental debugging technique. Use `System.out.println()` to act as a window into your code's execution. You can verify variable values and track the flow of your program.

// Use print statements to check variable values
double armAngle = armEncoder.getAngle();
System.out.println("Current Arm Angle: " + armAngle);

// Use print statements to track code flow
if (armAngle > 90.0) {
    System.out.println("Arm is past 90 degrees, stopping motor.");
    armMotor.stop();
}

Step 4: Isolate and Simplify the Problem

If the error is in a complex part of your code, try to simplify it. Comment out sections of code using `//` or `/* ... */` to narrow down which part is causing the issue. Can you create a smaller, simpler test case that reproduces the same bug?

Step 5: Ask for Help (After You've Tried!)

If you've followed the steps above and are still stuck, it's time to ask a mentor. Be prepared to explain the problem clearly:

  • What were you trying to achieve?
  • What did you expect to happen?
  • What actually happened? (Show the error message!)
  • What steps have you already taken to troubleshoot?

Test Your Knowledge

Question: Your robot's code compiles but crashes as soon as you enable it in teleop. What is the absolute first thing you should do?