function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Carter85Carter85 

Need help trying to pull an ID from a list contained in a wrapperClass

I'm trying to pull a list of record IDs currently encapsulated in a wrapperClass for use in a different method but I'm coming across an annoying error.  The method I'm trying to execute in it's current state is below:
public pageReference tapplyPay(){
    	system.debug(tcontractList);
    	
    	list<MG_Contract_Holder__c> temp = new list<MG_Contract_Holder__c>();
    	list<ID> temp2 = new list<ID>();
    	apply = tpayAmount/tcontractList.size();
    	
    	system.debug(tcontractList[0].voidID);
    	    	
    	for(integer i = 0; i <= tcontractList.size(); i++){
    		temp2.add(tcontractList[i].voidID);
    		}
    	
    	system.debug(temp2);
    	
    	temp = [SELECT ID, Contract_Status__c, Remit_Amount__c FROM MG_Contract_Holder__c WHERE ID IN:temp2];
    	    	 
        for(integer i = 0; i <= temp.size(); i++){
            	temp[i].Contract_Status__c = 'Submitted - Pending Payment';
                temp[i].Remit_Amount__c = apply;
                }        
    	
    	update(temp);
    
    	PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
		pageRef.setRedirect(true);
		return pageRef;
    	}
Where tcontractList is encapsulated by the wrapperClass.
The error I encounter is with the line:
temp2.add(tcontractList[i].voidID);
It throws an List index out of bounds error even though with the debug statement above, system.debug(tcontractList[0].voidID);, I know there's at least one value it should be finding when I try and iterate through the tcontractList and assign the IDs to my other list.
Is there some other way to do this with wrappers that I'm missing or can anyone see my error to advise?
Any help appreciated.

 
Best Answer chosen by Carter85
James LoghryJames Loghry

The issue you are having is using <= in your for loop.

Most collections in programming are zero indexed, and the last element your accessing doesn't exist, which throws your index out of bounds exception.

Say you have a list size of 1.  Because you are using <=, your for loop will iterate two times, looking at list(0), and list(1), where only list(0) exists.

Changing your condition to < instead of <= OR using the for each syntax will fix your problem.

All Answers

James LoghryJames Loghry

The issue you are having is using <= in your for loop.

Most collections in programming are zero indexed, and the last element your accessing doesn't exist, which throws your index out of bounds exception.

Say you have a list size of 1.  Because you are using <=, your for loop will iterate two times, looking at list(0), and list(1), where only list(0) exists.

Changing your condition to < instead of <= OR using the for each syntax will fix your problem.

This was selected as the best answer
Carter85Carter85
That did indeed fix the problem.  Thanks.