New to Java programing. Working on planning some Auto routines. We have successfully followed a path from PathWeaver with RamseteCommand created in the getAutonomousCommand and returning that command (like the wpilib documents show). We are attempting to implement a SequentialCommandGroup so that we can run the shooter for a time and then follow a path using RamseteCommand to pick up some additional cargo and make more complex routines. I have used the exact same RamseteCommand that worked when in the getAutonomousCommand, but it is generating an error every time it goes to start running the command.
I am getting this error:
Unhandled exception: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
from: java.base/jdk.internal.util.Preconditions.outOfBounds(Unknown Source)
at: at java.base/jdk.internal.util.Preconditions.outOfBounds(Unknown Source)
at: at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Unknown Source)
at: at java.base/jdk.internal.util.Preconditions.checkIndex(Unknown Source)
at: at java.base/java.util.Objects.checkIndex(Unknown Source)
at: at java.base/java.util.ArrayList.get(Unknown Source)
at: at edu.wpi.first.math.trajectory.Trajectory.sample(Trajectory.java:99)
at: at edu.wpi.first.wpilibj2.command.RamseteCommand.initialize(RamseteCommand.java:137)
at: at edu.wpi.first.wpilibj2.command.SequentialCommandGroup.execute(SequentialCommandGroup.java:72)
at: at edu.wpi.first.wpilibj2.command.CommandScheduler.run(CommandScheduler.java:280)
at: at frc.robot.Robot.robotPeriodic(Robot.java:74)
at: at edu.wpi.first.wpilibj.IterativeRobotBase.loopFunc(IterativeRobotBase.java:328)
at: at edu.wpi.first.wpilibj.TimedRobot.startCompetition(TimedRobot.java:131)
at: at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:373)
at: at edu.wpi.first.wpilibj.RobotBase.startRobot(RobotBase.java:463)
at: at frc.robot.Main.main(Main.java:23)
Error at java.base/jdk.internal.util.Preconditions.outOfBounds(Unknown Source): Unhandled exception: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Unknown Source)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Unknown Source)
at java.base/jdk.internal.util.Preconditions.checkIndex(Unknown Source)
Here is a link to the github for the TestAuto command. The rest of the code is there as well.
There is alot of cleanup and unused imports and things as this is a work in progress so go easy we are new to Java
Agreed, the trajectory is empty when the autonomous command is created.
In the constructor for RobotContainer on line 93, testAuto variable gets set to a new instance of the command group with the trajectory from Robot. However, in Robot.java on line 42 the robotContainer variable is initialized to a new robot container before the trajectories are loaded, which means the testTrajectory is still an empty trajectory when the autonomous command is created.
I would try loading the trajectory before initializing the robot container. (so move lines 44 through 49 in Robot.java above line 42)
Sorry this reply was in two parts, I sent it before I was done typing by accident lol.
That makes perfect sense. I will give it a try and post back on here tomorrow… getAutonomousCommand wasnt run until well after the paths were imported.
Thanks! Wish I understood the error messages better
Don’t be deterred by the error’s stack trace being that long. The key elements are
Unhandled exception:
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
...
java.base/java.util.ArrayList.get(Unknown Source)
...
edu.wpi.first.wpilibj2.command.RamseteCommand.initialize(RamseteCommand.java:137)
So the RamseteCommand has trouble early on in initialize, something related to an array.
If you check the source code for RamseteCommand.java line 137, it’s this:
var initialState = m_trajectory.sample(0);
So for some reason the trajectory must be empty, and it’s downhill from there on.
Error at java.base/java.util.ImmutableCollections$Set12.(Unknown Source): Unhandled exception: java.lang.NullPointerException
at java.base/java.util.ImmutableCollections$Set12.(Unknown Source)
at java.base/java.util.Set.of(Unknown Source)
at edu.wpi.first.wpilibj2.command.CommandGroupBase.requireUngrouped(CommandGroupBase.java:57)
at edu.wpi.first.wpilibj2.command.ParallelRaceGroup.addCommands(ParallelRaceGroup.java:37)
at edu.wpi.first.wpilibj2.command.ParallelRaceGroup.(ParallelRaceGroup.java:32)
at frc.robot.commands.TestAuto.(TestAuto.java:28)
at frc.robot.RobotContainer.getAutonomousCommand(RobotContainer.java:134)
at frc.robot.Robot.autonomousInit(Robot.java:95)
at edu.wpi.first.wpilibj.IterativeRobotBase.loopFunc(IterativeRobotBase.java:294)
at edu.wpi.first.wpilibj.TimedRobot.startCompetition(TimedRobot.java:131)
at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:373)
at edu.wpi.first.wpilibj.RobotBase.startRobot(RobotBase.java:463)
at frc.robot.Main.main(Main.java:23)
DifferentialDrive… Output not updated often enough.
Error at edu.wpi.first.wpilibj.MotorSafety.check(MotorSafety.java:96): DifferentialDrive… Output not updated often enough.
I am struggling to figure out why it is doing this only in Autonomous Mode. When I enable during teleoperated no error is given. I updated the github with new code. The auto I am trying is the TestAuto Command