+ Start a Discussion
GeneEAGeneEA 

Exception - List index out of bounds

Hello.

I have a VF page that displays 2 lists via the apex:repeat method. The items on the list can be deleted when clicking on a link that invoked the following method:

public PageReference deleteAdditionalCategory(){
        PageReference p = null;
        try{
            String categoryId = ApexPages.currentPage().getParameters().get('CategoryID');
            if(vendorAccountMembershipDetails != null && categoryId != null){
                Category__c c = mapCatList.get(categoryId);
                if(c != null){
                  mapCatList.remove(c.Id);
              	  
              	  Integer lSize = 0;
                  if(addCat != null && addCat.size() > 0){
                  	lSize = addCat.size();
                	for(Integer i=0; i < lSize; i++){
	                	if(addCat[i].Id == c.Id)
	                		addCat.remove(i);
	                		//catList.remove((i+1));
                	}
                  }
                  if(catList != null && catList.size() > 0){
                  	lSize = catList.size();
                	for(Integer j=1; j < lSize; j++){
	                	if(catList[j].Id == c.Id)
	                		catList.remove(j);
                	}
                  }
                  delete c;
                }
            }
            goAction();
        }catch(Exception e){
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Exception - ' +e.getMessage());
            ApexPages.addMessage(msg);
            system.debug('------Exception caught----'+e);
            if(vendorAccountMembershipDetails != null){
                //sendExceptionEmail(vendorAccountMembershipDetails, e);
            }    
        }
        
        return p;
    }

 

 The problem is, the first list (addCat) gets updated but the second one (catList) doesn't. The difference between the two lists is that addCat contains all additional categories associated with a membership order form while catList contains all additional categories plus the primary category. This way, catList will always be larger by 1.

The error I am getting is: Exception - List index out of bounds: followed by the index number of the item I am trying to remove from the list. Here's the odd part... The method will correctly remove items from the lists if I am removing the item at the end of the list. If I try deleting something in the middle, that's when the exception happens.

Best Answer chosen by Admin (Salesforce Developers) 
aballardaballard

Assuming there is only one entry in the list for the specified id, you should exit the loop when you find the match and remove it. 

 

(As it is, once you have removed it, the list is one smaller, so the limit lsize is too big and it will get an error last time around the loop... all working as expected...)

All Answers

aballardaballard

Assuming there is only one entry in the list for the specified id, you should exit the loop when you find the match and remove it. 

 

(As it is, once you have removed it, the list is one smaller, so the limit lsize is too big and it will get an error last time around the loop... all working as expected...)

This was selected as the best answer
GeneEAGeneEA

That did it. Thank you!