I am not quite sure why i am getting an exception in this method to begin with and why at this location? (No modification happens there)
Iterator<EventGroup> groupIterator = eventGroups.iterator();
while (groupIterator.hasNext()) {
EventGroup eventGroup = groupIterator.next();
Iterator<ScheduledEvent> eventIterator = eventGroup.getEvents().iterator();
while (eventIterator.hasNext()) {
ScheduledEvent event = eventIterator.next(); // <------------- ConcurrentModificationException
boolean found = ((SomeObject) event).getSomeId() == someId;
if (found) {
unschedule++;
unscheduleEvent(event);
eventGroup.remove(event);
if (eventGroup.isEmpty()) {
eventGroups.remove(eventGroup);
}
}
}
}
It seems that the only thing being modified here is eventGroup, but i am using an iterator to walk over it.
Please help me understand what’s happening here?
eventIteratoris an iterator oneventGroupand you calleventGroup.remove(event);while iterating. You should use the iterator instead to remove the item:This behaviour is explained in the javadoc of ArrayList:
Note: you have the same issue with
eventsGroupand you should replaceeventGroups.remove(eventGroup);withgroupIterator.remove();.