0

I tried to build my first Android app (a noobish Snake remake) and I noticed the program always crashes once my Snake reaches a length of 3.

Every part of my snake is stored in a class called "Segment", which stores the x and y coordinates and the lifetime of this bodypart.

static List<Segment> list = new ArrayList<Segment>();{  //my list
    Segment startingSegment = new Segment(0, 0, 1);     //adding starting element to my list
    list.add(startingSegment);
}

For test reasons I made a special loop which adds elements to my list:

while(list.size() < 2){                          //this works
    Segment newSegment = new Segment(0, 0, 1);
    list.add(newSegment);
}

while(list.size() < 3){                          //this crashes the app already
    Segment newSegment = new Segment(0, 0, 1);
    list.add(newSegment);
}

Logcat:

    04-05 15:50:19.921: E/AndroidRuntime(14308): FATAL EXCEPTION: Thread-3556 04-05 
15:50:19.921: E/AndroidRuntime(14308): Process: com.example.snake, PID: 14308 04-05
 15:50:19.921: E/AndroidRuntime(14308): java.util.ConcurrentModificationException 04-05 
15:50:19.921: E/AndroidRuntime(14308): at 
java.example.ArrayList$ArrayListIterator.next(ArrayList.java:573)

I really don't know what's going on there and would appreciate some help.

StarsSky
  • 6,721
  • 6
  • 38
  • 63
user3501232
  • 47
  • 1
  • 7
  • 04-05 15:50:19.921: E/AndroidRuntime(14308): FATAL EXCEPTION: Thread-3556 04-05 15:50:19.921: E/AndroidRuntime(14308): Process: com.example.snake, PID: 14308 04-05 15:50:19.921: E/AndroidRuntime(14308): java.util.ConcurrentModificationException 04-05 15:50:19.921: E/AndroidRuntime(14308): at java.example.ArrayList$ArrayListIterator.next(ArrayList.java:573) – user3501232 Apr 05 '14 at 13:51
  • 1
    Read more from the logcat and it will say to you the line, anyway the problem is caused by the fact you are adding/removing elements from the arraylist while you read it using an iterator – Marco Acierno Apr 05 '14 at 13:53
  • try synchronized in all function which is using list – Faisal Ali Apr 05 '14 at 13:54
  • This can help you if you have a concurrency issues http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html – Faisal Ali Apr 05 '14 at 13:54
  • You should use Iterator – Vetalll Apr 05 '14 at 13:56
  • @Vetal.lebed That won't neccessarily protect you from concurrent modification exception. – Fildor Apr 05 '14 at 13:58
  • 1
    see here: http://stackoverflow.com/questions/6866238/concurrent-modification-exception-adding-to-an-arraylist – rcbevans Apr 05 '14 at 14:00
  • Thank you for your link o0rebelious0o, that was exactly what I was looking for. Program works fine now c: – user3501232 Apr 06 '14 at 10:16

2 Answers2

1

I really don't know what's going on there and would appreciate some help.

What's going on here is that you've created a shared List object, and are trying to access it from multiple threads without any proper synchronization. This results in exception. Consider using synchronized list wrapper around your list:

List<Segment> list = Collections.synchronizedList(new ArrayList<Segment>());

That should solve your problem in case you only add and remove items. If you're going to iterate the list, you will also have to play a bit more with synchronization, as the iteration on a synchronized collection is not thread-safe.

Drew
  • 3,307
  • 22
  • 33
0

The problem was that I was removing items from my list while iterating through it. Making a second "toRemove" list fixed it.

user3501232
  • 47
  • 1
  • 7