OutOfBoundsException

We are getting this error when we deploy our code. Anyone got ideas on what to check next?

We have a zip file of our code if you need it. It seems our first encoder loads but our second one fails. Line 41 is our second encoder.

Unhandled exception: java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2  frc.robot.subsystems.DriveTrainSub.(DriveTrainSub.java:41) 
 Error at frc.robot.subsystems.DriveTrainSub.(DriveTrainSub.java:41): Unhandled exception: java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2 
 at frc.robot.subsystems.DriveTrainSub.(DriveTrainSub.java:41)

It appears that you are trying to access an array element that is beyond the scope of your current array. Remember, in Java (and most programming languages), arrays start at 0, not 1.

If you post your code we will have a much easier time diagnosing your problem. The easiest way to share is through a free service like github.com.

1 Like

Uploading to github now.

Link to our code.

Okay, so you are indeed indexing outside of your array. In your Constants class you have two static final integer arrays for your left and right encoders. Each have a length of two with two elements initialized.

Your issue will be fixed if you change line 42 of your DriveTrainSub class to have the same indexing scheme as line 37 of the same class.
new Encoder(DriveConstants.kRightEncoderPorts[0], DriveConstants.kRightEncoderPorts[1],

2 Likes

@Brian_Michell is correct. You are going out of bounds on your array.

Your constants.java:

    public static final int[] kLeftEncoderPorts = new int[] {0, 1};
    public static final int[] kRightEncoderPorts = new int[] {2, 3};

Each array only has 2 elements.

You probably want …

new Encoder(DriveConstants.kRightEncoderPorts[0], DriveConstants.kRightEncoderPorts[1],

.

Thanks guys for the help. Multiple eyes always helps.

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