JAVAWarningRuntime ErrorJune 6, 2026

Runtime Error

java.util.ConcurrentModificationException: null at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1034) at java.base/java.util.ArrayList$Itr.next(ArrayList.java:954)

What This Error Means

This error occurs when a program attempts to modify a collection (like an ArrayList or HashSet) while it is being iterated over by a foreach loop, iterator, or lambda expression.

Why It Happens

This error often happens when multiple threads are accessing the same collection concurrently, or when a program is using a collection in a multithreaded environment without proper synchronization.

How to Fix It

  1. 1To fix this error, you can use the following solutions:
  2. 21. Use an Iterator to remove elements while iterating over a collection.
  3. 32. Create a temporary collection to store the elements to be removed.
  4. 43. Use a thread-safe collection, such as CopyOnWriteArrayList or ConcurrentHashMap.
  5. 54. Synchronize access to the collection using a lock or a semaphore.

Example Code Solution

❌ Before (problematic code)
Java
import java.util.ArrayList;
import java.util.Iterator;

public class ConcurrencyErrorExample {
  public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    list.add("C");

    for (String s : list) {
      if (s.equals("B")) {
        list.remove(s);
      }
    }
  }
}
✅ After (fixed code)
Java
import java.util.ArrayList;
import java.util.Iterator;

public class ConcurrencyErrorExample {
  public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    list.add("C");

    Iterator<String> iterator = list.iterator();
    while (iterator.hasNext()) {
      String s = iterator.next();
      if (s.equals("B")) {
        iterator.remove();
      }
    }
  }
}

Fix for java.util.ConcurrentModificationException: null at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1034) at java.base/java.util.ArrayList$Itr.next(ArrayList.java:954)

Related JAVA Errors

Related JAVA Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error