Concurrent modification exception error

we were just testing our code for the color sensor. We have two buttons that schedule the command when we press them:

    @Override
    public void teleopInit() {
        container.initTeleopCommands();
        leftStick.getButton5().whenPressed(new SpinWheelThreeTimes(), false);
        leftStick.getButton6().whenPressed(new SpinToColor(), false);
    }

    @Override
    public void teleopPeriodic() {
        CommandScheduler.getInstance().run();
    }

pressing button 6 will always run fine, however when we try to run button 5, the code will always crash with this exception ConcurrentModificationException. I don’t currently have the laptop with the stacktrace with me, however it was pretty similar to this thread. However the stacktrace looked something like this from the best of my memory:

Error at java.base/java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:719): Unhandled exception: java.util.ConcurrentModificationException
        at java.base/java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:719)
        at java.base/java.util.LinkedHashMap$LinkedKeyIterator.next(LinkedHashMap.java:741)
        at edu.wpi.first.wpilibj2.command.CommandScheduler.run(CommandScheduler.java:243)
        at com.rambots4571.infiniterecharge.robot.Robot.teleopPeriodic(Robot.java:49)
        at edu.wpi.first.wpilibj.IterativeRobotBase.loopFunc(IterativeRobotBase.java:220)
        at edu.wpi.first.wpilibj.TimedRobot.startCompetition(TimedRobot.java:82)
        at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:276)
        at edu.wpi.first.wpilibj.RobotBase.lambda$startRobot$0(RobotBase.java:329)
        at java.base/java.lang.Thread.run(Thread.java:834)

Both commands use the same subsystem, I even tried to remove the addRequirements() from both of the commands to see fi that was the problem. But that didn’t stop it. The only way we managed to get it working is by commenting out the button 6 command, so button 5 would work:

    @Override
    public void teleopInit() {
        container.initTeleopCommands();
        leftStick.getButton5().whenPressed(new SpinWheelThreeTimes(), false);
        // leftStick.getButton6().whenPressed(new SpinToColor(), false);
    }

    @Override
    public void teleopPeriodic() {
        CommandScheduler.getInstance().run();
    }

We are currently running this with the WPILIB 2020.2.2 version

you can view the full project here

Have you updated WPILib to the latest?

There was a bug earlier this year with an iterator in the scheduler that caused this very exception.

we’re currently on 2020.2.2

The only thing that could cause this is if you call CommandScheduler.addButton() or CommandScheduler.clearButtons() from within a button’s run() function.

You really should call CommandScheduler.getInstance().run() in robotPeriodic only. You want to run the command framework (and subsystems) when you’re disabled or in test mode too.

You should not add button to command mappings in teleopInit(). The teleopInit() method is called each time your robot enters teleop. During a match that should only happen once between code restarts. However, while testing, you could easily be moving in and out of teleop multiple times between code restarts. This would result in you having multiple instances of each command mapped to the same button. I have seen this cause all sorts of odd behavior before.

1 Like

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