I believe I have figured it out. Well at least for the past 100+ times I have opened my game it has not occurred.
Let me brake down the basics of what I was doing first. Inside my Game class is where I initialize all my handlers, so for instance my ComponentHandler. After my handlers are initialized I initialize the starting State of the game, in this case the MainMenuState. OK, so inside of the ComponentHandler constructor is where I add my MouseListener and MouseMotionListener to game, therefore the mouseMoved() method starts getting called before the State of the game is initialized. Well the problem doesn't exactly have to do with the order of my initializations, but it did play an important factor. This is what my mouseMoved() method looked like before the fix:
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int componentId = -1;
for (Component c : components) {
if (c == null || c.isDisabled())
continue;
if (c.contains(x, y)) {
componentId = c.getId();
if (!c.getHovered())
c.setHovered(true);
} else
c.setHovered(false);
}
if (componentId != -1) {
switch(componentId) {
// TODO: HANDLE THINGS
}
}
}
So as you can see that it will start to loop through all the components in order to find the componentId of whatever the x, y coordinates of the mouse are contained within.
Ok so now that the ComponentHandler is initialized, its time to initialized my MainMenuState. Well inside my MainMenuState is where the Interface is built for the main menu. This includes adding Components (Buttons) to the interface. Every Button() extends the Component class and upon creation is automatically added to the List of components inside ComponentHandler. So this is where the exception is thrown, while I am already iterating through the list of components inside ComponentHandler, I am also trying to add new buttons to that list through the creation of the main menu interface. Since you can't add or remove items to a list while iterating through it a ConcurrentModificationException was thrown.
How I decided to fix this problem is by stopping the mouseMoved() from iterating through the List without the State of the game first being initialized like so:
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int componentId = -1;
if (State.currentState == null || !State.currentState.isInitialized()) // Fix
return;
for (Component c : components) {
if (c == null || c.isDisabled())
continue;
if (c.contains(x, y)) {
componentId = c.getId();
if (!c.getHovered())
c.setHovered(true);
} else
c.setHovered(false);
}
if (componentId != -1) {
switch(componentId) {
// TODO: HANDLE THINGS
}
}
}
Inside the current States class I just be sure to set the initialized boolean to true at the end of the constructor. Also at the beginning of every States initialization I clear the list of components inside ComponentHandler. And just for the record I did the same thing with the mousePressed() to avoid any issues with that.
I am not 100% sure if this was the cause, however it seems to be fixed for now without the use of any CopyOnWriteArrayLists If you have any questions, comments or concerns let me know.