ArrayList.clear throws ArrayIndexOutOfBoundsException

Viewed 466

Getting below exception on ArrayList.clear() Any reason why would clear throw such exception. I know add or get might throw this for negative index or larger thn list size.

Fatal Exception: java.lang.ArrayIndexOutOfBoundsException: length=49; index=49
       at java.util.ArrayList.clear(ArrayList.java:569)
1 Answers

The implementation of ArrayList is not thread safe by design. Specifically regarding clear(), the code assumes that the size of the list doesn't change while clear is executing.

Most likely, you have another thread that reduces the the list size concurrently with clear which causes the logic to try and access an index that is out of bounds.

Related