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-/Robot.java at master · 7657thunderbots/PID-Loop-Gyro-V1-latest- (github.com)

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(Auto2_balance.java:30): Unhandled exception: java.lang.NullPointerException: Cannot invoke “frc.robot.DriveTrain.getAverageEncoderDistance()” because “this.drivetrain” is null
at frc.robot.Auto2_balance.Run_Auto2_balance(Auto2_balance.java:30)
at frc.robot.Robot.autonomousPeriodic(Robot.java:148)
at edu.wpi.first.wpilibj.IterativeRobotBase.loopFunc(IterativeRobotBase.java:344)
at edu.wpi.first.wpilibj.TimedRobot.startCompetition(TimedRobot.java:130)
at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:343)
at edu.wpi.first.wpilibj.RobotBase.lambda$startRobot$0(RobotBase.java:413)
at java.base/java.lang.Thread.run(Thread.java:833)

Warning at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:358): 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(RobotBase.java:365): The startCompetition() method (or methods called by it) should have handled the exception above.
Warning at edu.wpi.first.wpilibj.IterativeRobotBase.printLoopOverrunMessage(IterativeRobotBase.java:387): Loop time of 0.02s overrun

Error at edu.wpi.first.wpilibj.MotorSafety.check(MotorSafety.java:139): 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 Auto2_balance.java , 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-/Auto2_balance.java 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

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.