0

Trying to wrap up a project. Everything works in it except it can't instantiate the animal into the designated array. This line is code that my instructor gave me, so I'm at even more of a loss. This is what I used to declare the array list:

private static ArrayList<Dog> dogList = new ArrayList<Dog>();

I then collect the information I needed and use the code below to try and add it to the array list. nthDog is a previously declared variable and the parameters are correct.

 Dog nthDog = new Dog(name, breed, gender, age, weight, acquisitionDate,
            acquisitionCountry, trainingStatus, reserved, inServiceCountry);
 dogList.add(nthDog);

This code was sent to me by my instructor, however, when used it pulls up the errors ConcurrentModificationException and checkForComodification. Can't figure out what could be wrong.

4
  • 2
    We need more code to see the cause for that sort of error, ideally to see all the places dogList is used, or more context around the add call. Commented Aug 18, 2021 at 18:26
  • 1
    random guess until then: are you adding to dogList while iterating over it? Commented Aug 18, 2021 at 18:29
  • ConcurrentModificationException requires there to be an Iterator, either explicitly or implicitly e.g. with a for-each loop or a copy constructor. Please can you show the code doing the iterating? Commented Aug 18, 2021 at 18:55
  • Please provide a minimal reproducible example. Commented Aug 18, 2021 at 18:57

1 Answer 1

0

You might be modifying a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.

For more details on ConcurrentModificationException Please refer https://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.