Can't deploy robot code

When I try to deploy code to the robot, it deploys fully, but then in the RioLog, it says: "

  • Error at frc.robot.drivetrain.DriveSubsystem.(DriveSubsystem.java:126): Unhandled exception: java.lang.NullPointerException: Cannot invoke “edu.wpi.first.math.geometry.Rotation2d.unaryMinus()” because “other” is null

  • at edu.wpi.first.math.geometry.Rotation2d.minus(Rotation2d.java:138)

  • at edu.wpi.first.math.kinematics.Odometry.(Odometry.java:44)

  • at edu.wpi.first.math.kinematics.SwerveDriveOdometry.(SwerveDriveOdometry.java:36)

  • at edu.wpi.first.math.estimator.SwerveDrivePoseEstimator.(SwerveDrivePoseEstimator.java:79)

  • at edu.wpi.first.math.estimator.SwerveDrivePoseEstimator.(SwerveDrivePoseEstimator.java:49)

  • at frc.robot.drivetrain.DriveSubsystem.(DriveSubsystem.java:126)

  • at frc.robot.RobotContainer.(RobotContainer.java:30)

  • at frc.robot.Robot.robotInit(Robot.java:36)

  • at edu.wpi.first.wpilibj.TimedRobot.startCompetition(TimedRobot.java:107)

  • at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:365)

  • at edu.wpi.first.wpilibj.RobotBase.startRobot(RobotBase.java:453)

  • at frc.robot.Main.main(Main.java:27)"

how do i fix this??

its a error on your code. can you provide it so we can take a look ?

yeah sorry i completely forgot to link it: GitHub - Mateo-Johnson/RobustSturdy3

and this is the specific file referenced to

You need to initialize the getHeadingPose2d field. It is null by default, and is not initialized by the time it’s used when constructing the odometry object. You can either set it to zero, or set it to the gyro’s heading immediately before using it in the odometry object

wait so where and how would i do that? i don’t really understand.

and also, the getHeadingPose2d field is line 123, while the error it cites me is on line 126. is that just because thats the line where the constructor ends?

On line 120, set getHeadingPose2d = Rotation2d.fromDegrees(gyro.getHeading());

This will initialize the field before you pass it (erroneously, as null) to the odometry object. Line 123 uses the field, but if it’s not initialized it will be null, which causes the crash because the pose estimator expects it to be an actual value

that makes sense thank you so much!!

1 Like

do i need to use the gyro.getheading or can i use the drivesubsystem function getheading?

Wherever your gyro object lives, you want to getHeading() from that

wait so i cant use the direct method that gets it from the gyro in the subsystem?

You’d be able to use the getHeading() method from the subsystem. I misremembered your code when I wrote out the example

alright sounds good!!