Balwinder SIngh
Balwinder SIngh

Reputation: 1961

ArrayList.clear throws ArrayIndexOutOfBoundsException

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)

Upvotes: 2

Views: 1389

Answers (1)

David Soroko
David Soroko

Reputation: 9096

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.

Upvotes: 5

Related Questions