ArrayList.add throws ArrayIndexOutOfBoundsException [duplicate]

冷暖自知 提交于 2019-12-03 05:21:34
Dave Webb

ArrayList.add() should never throw an ArrayIndexOutOfBoundsException if used "properly" so it seems that you're using your ArrayList in a way which it does not support.

It's hard to tell from just the code you've posted but my guess is that you're accessing your ArrayList from multiple threads.

ArrayList isn't synchronised and so isn't thread safe. If this is the problem you can fix it by wrapping your List using Collections.synchronizedList().

Changing your code to the following should resolve the problem:

private void populateInboxResultHolder(List inboxErrors){
    List inboxList = Collections.synchronizedList(new ArrayList());
    try{                
        inboxHolder = new InboxResultHolder();
        //Lots of Code
        inboxList.add(inboxHolder);
    }catch(Exception e){
        e.printStackTrace();
    }
}

The code you posted will not throw ArrayIndexOutOfBoundsException.

The exception you get is thrown in the part you omitted. Take a look at your stacktrace. Its InboxSearchBean that causes the exception. Most likely it performs a get(index) on the list with an invalid index.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!