You need to sign in to do that
Don't have an account?
List index out of bounds: 1
Hi, I am trying to sum up the values of a number field in an object for a particular function, when attempting to do that I encounter an error message in the for loop. The section of code is as below
Object1__c[] obj1 = [select Id, Name, Qty__c from Object1__c where lookup1id__c = :param1 and lookup2id = :param2]; for(Integer i = 0; i <= obj1.size(); i++) {
totalqty = totalqty + obj1[i].Qty__c;
}
here param1 and param2 are values that are passed from the UI to the controller. This error appears inside the forloop for the condition. I am not sure why and how to handle this.
Thanks
KD
1. Did you mean lookup2id__c in your query?
2. Did you initialize totalqty?
If those aren't the problems, post more data.
-S
Yes I did mean lookup2id__c in the query and have also initialised totalqty. I have a visualforce page and this is the extension class. I am try to do a check on the save function to identify if the Qty is within the limitis.
This loop will always go 1 too many
for(Integer i = 0; i <= obj1.size(); i++)
you need
for(Integer i = 0; i < obj1.size(); i++)
Thanks Simon, Thanks Steve........ for the same case of summing up a field value will this sequence of steps work, or do you see anything wrong.
obj1list = [select Id, Name, Qty__c from Object1__c where lookup1id__c =: param1 and lookup2id =: param2];
for(Object1__c o1 : obj1list) {
totalqty = totalqty + o1.Qty__c;
}
Which of the 2 methods would be correct and appropriate.
Thanks
KD