Unit 3: Version Control and Troubleshooting
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.
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.
Don't get frustrated. Finding and fixing bugs is a normal and expected part of programming. Every error is a learning opportunity.
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.
While external factors can cause issues, the bug is almost always in the code you've written. This mindset helps you focus your search.
When your program isn't working, follow these steps to systematically find and fix the issue.
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.
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.
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();
}
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?
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:
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?