Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Brizton Floyd
Brizton Floyd
3,348 Points

Runtime Exception Only In WorkSpaces runs fine using IDE

Im not sure why im getting this error. My code compiles fine but at runtime im getting this exception. It's only occurring in workspaces and im not sure why. I have Intellij Idea and Eclipse and the code compiles and runs with no problem. I want to submit my project for review but don't want to loose points based on this issue here in workspaces. Does anyone have any idea about this ? As you can see from the stack trace the error is not coming from my custom class but seems to be coming from the ArrayList Collection somewhere.

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at com.teamtreehouse.model.Roster.removeFromRoster(Roster.java:46)
at com.teamtreehouse.model.Coach.removePlayerFromRoster(Coach.java:125)
at com.teamtreehouse.model.Organizer.removePlayerFromTeam(Organizer.java:159)
at com.teamtreehouse.model.Organizer.makeSelection(Organizer.java:32)
at LeagueManager.main(LeagueManager.java:26)

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

From Oracle docs: https://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html

Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.

I'm guessing that you're using a for-each loop to search for an item to remove and remove it inside of the loop. This can cause errors because you're changing the iteration order while you are still iterating over the list.

Perhaps this SO thread will help: http://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re

Brizton Floyd
Brizton Floyd
3,348 Points

Hello Seth I read the oracle docs and used an Iterator and it stopped the error. However when I execute the code in workspaces I still get the error. And when I run the code on intelli J Idea I don't get the error.

Seth Kroger
Seth Kroger
56,413 Points

Well, let's take a look at your code then.