3

I am trying to update at once fields in amount of document, I want to modify "download=0" where "md5" is in the list. When I run the code, it throws an exception:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)

This is what I am doing:

//the list is an list of "md5"

List<BasicDBObject> updateCondition=new ArrayList< BasicDBObject>(list.size());  
for(int i = 0 ; i < list.size(); i++) {
    updateCondition.get(i).put("md5", list.get(i));
    DBObject updatedValue=new BasicDBObject();  
    updatedValue.put("download", 0);   
    DBObject updateSetValue=new BasicDBObject("$set",updatedValue);               
    vt_col.update(updateCondition.get(i), updateSetValue);  
} 

The exception is occuring at :

 updateCondition.get(i).put("md5", list.get(i));

I want your help, thanks for helping me.

1 Answer 1

2

Debug would help you a lot here, I can't say with a 100% accuracy, but I think you updateCondition maybe is empty.

You are trying to access updateCondition(0)... but you just create it with:

new ArrayList< BasicDBObject>(list.size()); 

The exception is throwing cause you don't have any objects in the list still, first you have to copy the list to the updateCondition list.

Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

This answer is correct. To elaborate on why this happens: The constructor ArrayList<T>(int initialCapacity) does not create a list with initialCapacity entries, it creates an empty list with reserved memory for initialCapacity entries. Settng the reserved capacity of a collection is a pure performance-optimization but does not do anything regarding its behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.