Null Pointer Error when Invoking CANCoder.getAbsolutePosition()

So for reference I am following team 364’s BaseFalconSwerve to a certain extend if anyone already has some prior familiarity to that. I am trying to use the get absolute position method which throws a NPE because my CANCoder object is null. It it really confusing because the way I instantiate it is the same for my other objects and they work perfectly fine. Any help or comments at all is appreciated. Below I’ll provide both the link to my repo and the stacktrace:

Github: GitHub - rambots/Viina at NewDT

Stacktrace:
Error at com.rambots4571.chargedup.robot.utils.SwerveModule.getCanCoder(SwerveModule.java:121): Unhandled exception: java.lang.NullPointerException: Cannot invoke “com.ctre.phoenix.sensors.CANCoder.getAbsolutePosition()” because “this.angleEncoder” is null
at com.rambots4571.chargedup.robot.utils.SwerveModule.getCanCoder(SwerveModule.java:121)
at com.rambots4571.chargedup.robot.utils.SwerveModule.resetToAbsolute(SwerveModule.java:128)
at com.rambots4571.chargedup.robot.utils.SwerveModule.configTurnMotor(SwerveModule.java:150)
at com.rambots4571.chargedup.robot.utils.SwerveModule.(SwerveModule.java:40)
at com.rambots4571.chargedup.robot.subsystems.DriveTrain.(DriveTrain.java:41)
at com.rambots4571.chargedup.robot.subsystems.DriveTrain.(DriveTrain.java:32)
at com.rambots4571.chargedup.robot.RobotContainer.(RobotContainer.java:41)
at com.rambots4571.chargedup.robot.Robot.robotInit(Robot.java:59)
at org.littletonrobotics.junction.LoggedRobot.startCompetition(LoggedRobot.java:59)
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.
[phoenix-diagnostics] Server shutdown cleanly. (dur:0)

[phoenix] Library shutdown cleanly

You’re calling configTurnMotor from the constructor (on line 40) before the angleEncoder is initialized (which happens on line 42). This doesn’t work because configTurnMotor calls resetToAbsolute, which calls getCanCoder…

1 Like

Man I spent so long staring at my code wondering why it didnt work, thank you so much. I really need to get better at these types of things

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