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)
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