0

The following code is causing

java.util.ConcurrentModificationException

I am not sure how to solve the error please help !

ArrayList strList =  new ArrayList<String>(Arrays.asList(cmd.split(" ")));

        if (strList.get(0).equals("LIST")) {

        }

        if (strList.get(0).equals("DEPEND")) {
            strList.remove(0); // getting error at this point 
            cm.createComponent(strList);

        }

Full Method The outer loop is not related to the List

public static void main(String[] args) throws IOException {
        ComponentManager cm = new ComponentManager();

    List<String> lines = Files.readAllLines(Paths.get("cmdList.txt"));
    for (String cmd : lines) {
        ArrayList strList =  new ArrayList<String>(Arrays.asList(cmd.split(" ")));

        if (strList.get(0).equals("LIST")) {

        }

        if (strList.get(0).equals("DEPEND")) {
            strList.remove(0);
            cm.createComponent(strList);

        }

        if (strList.get(0).equals("INSTALL")) {

        }

        if (strList.get(0).equals("REMOVE")) {

        }

    }

}
10
  • you need to user iterator , Commented Sep 27, 2016 at 6:03
  • There's no iteration here. Commented Sep 27, 2016 at 6:04
  • 1
    give try removing first element by iterator Commented Sep 27, 2016 at 6:05
  • 2
    Can you show us full stack trace? It is like there is another problem. Commented Sep 27, 2016 at 6:18
  • 3
    @JimGarrison But since the list is wrapped in an ArrayList, the OP shouldn't be seeing UnsupportedOperationException either. Definitely a case of missing details from the question. Commented Sep 27, 2016 at 6:19

1 Answer 1

1

You can create a different ArrayList and perform remove operation there or put up an iterator on the arrayList and remove using the iterator.

Find a couple of potential solutions to your problem here and here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.