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

Error at com.rambots4571.chargedup.robot.utils.SwerveModule.getCanCoder( 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(
at com.rambots4571.chargedup.robot.utils.SwerveModule.resetToAbsolute(
at com.rambots4571.chargedup.robot.utils.SwerveModule.configTurnMotor(
at com.rambots4571.chargedup.robot.utils.SwerveModule.(
at com.rambots4571.chargedup.robot.subsystems.DriveTrain.(
at com.rambots4571.chargedup.robot.subsystems.DriveTrain.(
at com.rambots4571.chargedup.robot.RobotContainer.(
at com.rambots4571.chargedup.robot.Robot.robotInit(
at org.littletonrobotics.junction.LoggedRobot.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.
[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.