IndexOutOfBounds error when trying to run trajectory

When using a PathWeaver trajectory we are given an IndexOutOfRange error


Any advice?

Sounds like the trajectory has zero points in it? What are you passing in?

This is the trajectory:
image
The blue lines cover up the path but it is just a line straight forward 3 meters.

How are you loading/generating the trajectory? Like Peter said, the trajectory has 0 points in it so accessing any points will result in that error

I exported the paths i made in pathweaver into the deploy/paths directory, and then I get the trajectory from the JSON like this:

  private String straightLineTest = "paths/StraightLine3Meters.wpilib.json";
  public static Trajectory straightLineTrajectory = new Trajectory();

@Override
  public void robotInit() {
    // Instantiate our RobotContainer.  This will perform all our button bindings, and put our
    // autonomous chooser on the dashboard.
    m_robotContainer = new RobotContainer();
    CameraServer.startAutomaticCapture();

    //load trajectories from file
    try{
      Path straightLineTestPath = Filesystem.getDeployDirectory().toPath().resolve(straightLineTest);
      straightLineTrajectory = TrajectoryUtil.fromPathweaverJson(straightLineTestPath);
    } catch(IOException ex){
      DriverStation.reportError("Unable to open trajectory: " + straightLineTest, ex.getStackTrace());
    }

  }`Preformatted text`

What could be happening is that it never loads it and just uses the empty trajectory originally created, I’m not sure how to fix that. I’m not seeing any errors in driver station though.

When the trajectory is put onto a field simulation in robotInit() it appears fine. Could it be an error in how I’m accessing it in RobotContainer?

I found the solution. The robot container, and the auton that used my trajectory was being created before a trajectory was created from my JSON file. To fix, I moved the try statement that got my trajectory from a JSON in robotInit() above the line that creates my robotContainer. Thank you to everyone that helped!

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