+ Start a Discussion
DoondiDoondi 

List index out of bounds: 35

Hi,
I have below code for "{!RecordsPerPage}",
If user chooses 40 records to display and if there are only 35 records, Then I am getting this error.
How to avoid this?

Here is my controller for RecordsPerPage
 
public pageReference RecordsPerPage()
    {
        if (PageSize < total)
        {
            if (math.mod(total,PageSize)>0)
            {
                totalpagecount=total/PageSize+1;  
            }//if
            else 
            {
                totalpagecount=total/PageSize;
            }//else
        }//if
        else
        {
            totalpagecount=10;
        }
        Set<String> setIds = new Set<String>();
        PageNo = PageNoCount = 1;
        IsNext = IsLast = false;
        IsPrevious = IsFirst = true;
        List<Unit__c> accountList = getAccountsData(setIds);
        accountList = SortByFields(accountList, sortExpression, sortDirection);
        
        Records.clear();
        for(Integer i = 0; i < PageSize; i++){
            Records.add(accountList[i]);
        }
        return null;
    }

 
Ajay K DubediAjay K Dubedi
Hello Doondi,

I have reviewed your code and find that you have entered the size of the page which is greater than accountlist, so that's why it gives an exception list out of bound because it does not find any record in accountlist.

The main reason behind this is the size of accountlist is smaller as compared to the page size.

I hope this will going to help you.

Thank You.
Ajay Dubedi