3

I have a visualforce page with

<apex:commandButton value="Afficher" action="{!filterRecords}" reRender="table" immediate="true" />

in the constructor I have:

 public RCS(){
           counter = 0;
           rcsList = new list<T13_REPART_CS__c>(); 
           rcswrapperList = new list<rcswrapper>();
           for(Integer i=0;i<10;i++){
               rcswrapper rcsWrap = new rcswrapper(new T13_REPART_CS__c()); 
               counter++;
               rcsWrap.counterWrap = counter;
               rcswrapperList.add(rcsWrap); 

           }

    }

so I initially have 10 rows. When I click the button I want all rows to be deleted:

 public PageReference filterRecords(){

        Integer size = rcswrapperList.size();
        for(Integer i=0;i<size;i++){

             if(!rcswrapperList.isEmpty())
             {   rcswrapperList.remove(i);     
              }  
            }
          counter=0;
return null;
}

I get the following error: System.ListException: List index out of bounds: 5

3

The simplest way to remove all items from a list is to call the list's clear method.

Your code fails because the list is being made shorter as you go round the loop but the index you are passing to the remove method is growing. The quick fix would be to change remove(i) to remove(0) so you are always removing the first element of the list.

  • 1
    I answered the same thing. Deleting the answer now! – ajinkyah May 15 '15 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.