Unit 6: Intermediate FRC Coding
The Driver Station logs are a real-time record of what your robot is thinking. They are your window into the robot's brain and the first place you should look whenever something isn't working as expected.
The robot uses different ways to speak to us through the logs. Understanding the difference is crucial for effective troubleshooting.
This is the simplest form of logging. Any `System.out.println()` statement in your code is sent directly to the Driver Station logs as an `INFO` message. This is great for temporary, real-time debugging.
// In a command's execute() method:
System.out.println("Arm Angle: " + m_arm.getAngle());
WPILib provides specific methods for sending more formal, color-coded messages. These are more effective for permanent debugging messages you might leave in your code.
if (isSensorDisconnected) {
DriverStation.reportWarning("Arm encoder is disconnected!");
}
This is the most important log entry you will ever see. When a runtime error (an "exception") occurs, the robot prints a detailed message called a stack trace. It tells you exactly where in your code the error happened.
A stack trace can look intimidating, but it's a map that leads you directly to the bug. To find the problem, you must read the trace from the bottom up until you find the first line that references your team's code.
Error at frc.robot.subsystems.IntakeSubsystem.run(IntakeSubsystem.java:42):
java.lang.NullPointerException: Cannot invoke "edu.wpi.first.wpilibj.motorcontrol.Spark.set(double)" because "this.m_intakeMotor" is null
at frc.robot.subsystems.IntakeSubsystem.run(IntakeSubsystem.java:42)
at frc.robot.commands.IntakeCommand.execute(IntakeCommand.java:25)
at edu.wpi.first.wpilibj2.command.CommandScheduler.run(CommandScheduler.java:225)
...
at frc.robot.Robot.robotPeriodic(Robot.java:30)
...
at frc.robot.Main.main(Main.java:23)
Question: When a `NullPointerException` occurs and a stack trace is printed to the Driver Station logs, what is the most effective first step to finding the bug?