Debugging code

I am getting an error whenever I run auto. Can someone help me debug it? Here is a link to my code :
PID-Loop-Gyro-V1-latest-/ at master · 7657thunderbots/PID-Loop-Gyro-V1-latest- (

Here is the errors that show up in the terminal when running the code in a simulation.

auto selected: My Auto
Error at frc.robot.Auto2_balance.Run_Auto2_balance( Unhandled exception: java.lang.NullPointerException: Cannot invoke “frc.robot.DriveTrain.getAverageEncoderDistance()” because “this.drivetrain” is null
at frc.robot.Auto2_balance.Run_Auto2_balance(
at frc.robot.Robot.autonomousPeriodic(
at edu.wpi.first.wpilibj.IterativeRobotBase.loopFunc(
at edu.wpi.first.wpilibj.TimedRobot.startCompetition(
at edu.wpi.first.wpilibj.RobotBase.runRobot(
at edu.wpi.first.wpilibj.RobotBase.lambda$startRobot$0(
at java.base/

Warning at edu.wpi.first.wpilibj.RobotBase.runRobot( The robot program quit unexpectedly. This is usually due to a code error.
The above stacktrace can help determine where the error occurred.
See Reading Stacktraces — FIRST Robotics Competition documentation for more information.
Error at edu.wpi.first.wpilibj.RobotBase.runRobot( The startCompetition() method (or methods called by it) should have handled the exception above.
Warning at edu.wpi.first.wpilibj.IterativeRobotBase.printLoopOverrunMessage( Loop time of 0.02s overrun

Error at edu.wpi.first.wpilibj.MotorSafety.check( DifferentialDrive… Output not updated often enough. See Using the WPILib Classes to Drive your Robot — FIRST Robotics Competition documentation for more information.
[phoenix-diagnostics] Server shutdown cleanly. (dur:0)

[phoenix] Library shutdown cleanly

Per Reading Stacktraces — FIRST Robotics Competition documentation , the errors indicate that the issue occurs while running , on line 30, where a null pointer exception happens (specifically, this.drivetrain happens to be null when that occurs).

However, line 30 doesn’t actually have any references to this.drivetrain - PID-Loop-Gyro-V1-latest-/ at master · 7657thunderbots/PID-Loop-Gyro-V1-latest- · GitHub

Are you certain the code you provided through the github link is the same as what generated the error?

And, wherever the error is actually happening at - how do you expect this.drivetrain to get set to a non-null value? Have you confirmed that this desired behavior is actually happening?

Sorry forgot to update my GitHub

Just updated it now

1 Like

You should pass a DriveTrain to the Run_Auto2_balance constructor and store it in the member variable.

I see it on line 30 now. @gerthworm is 100% correct. Learning how to read stacktraces is a critical skill for any developer. That said, read that line and trace back through your code (specifically that class) and see where youre setting the value of drivetrain

Thank you all for your help!!

1 Like