Basic Debugging Techniques in FTC OnBot Java
Introduction
Most FTC bugs show up in hardware names, init order, or the main loop. This lesson covers how to read errors and narrow them down with telemetry.
OpMode layout
Map hardware before waitForStart(). Put repeating logic in while (opModeIsActive()).
Minimal OpMode for testing
@TeleOp
public class DebugExampleOpMode extends LinearOpMode {
private DcMotor leftMotor;
@Override
public void runOpMode() {
leftMotor = hardwareMap.get(DcMotor.class, "left_drive");
telemetry.addData("Status", "Initialized");
telemetry.update();
waitForStart();
while (opModeIsActive()) {
leftMotor.setPower(0.5);
telemetry.addData("Motor Power", leftMotor.getPower());
telemetry.update();
}
}
}
Telemetry while debugging
Print variables and sensor reads each loop when behavior is wrong.
Telemetry example
telemetry.addData("Encoder Position", leftMotor.getCurrentPosition());
telemetry.addData("Loop Count", loopCount);
telemetry.update();
Common errors
- Hardware name mismatch: Driver Station cannot find the device string.
- NullPointerException: Used a motor or sensor before
hardwareMap.get(). - Compile errors: Typos in variable names.
See gm0: Fundamental Concepts of Programming.
Null pointer example
Uninitialized leftMotor causes a crash on setPower:
leftMotor.setPower(1.0); // Throws error if leftMotor is null
Test one piece at a time
Comment out subsystems until the problem disappears, then add them back.
Isolation example
// Only test motor initialization
leftMotor = hardwareMap.get(DcMotor.class, "left_drive");
telemetry.addData("Status", "Motor mapped");
telemetry.update();
Debugging checklist
- Check your hardware configuration matches your code.
- Use descriptive variable names.
- Keep code modular and avoid large files.
- Test one change at a time.
- Use telemetry to monitor variables and states.
- Read error messages carefully and look up unfamiliar terms.
Practice Debugging
Try introducing a bug into the example OpMode (such as misspelling a hardware name or skipping initialization). Observe the error message and use telemetry to help you find and fix the bug.
- Misspell the hardware name in hardwareMap.get(). What error do you see?
- Comment out the initialization line for leftMotor. What happens when you run the OpMode?
- Add a telemetry statement for a variable that doesn’t exist. What error do you get?