If I upload a csv file and I want to insert that csv file data s in to Account object, I create a code but i got some errors. If I load 2 columns in csv and add 5 columns in apex class but i got a exception Array Index Out of Bound Exception

Apex class

public class FileUploader
  String[] filelines = new String[]{};
  public List<Account> contoupload;
  public Blob contentFile { get; set; }
  public String nameFile { get; set; }

  public PageReference ReadFile()
    filelines = nameFile.split('\n');
    contoupload = new List<Account>();
    for(Integer i=1;i<filelines.size();i++)
        String[] inputvalues = new String[]{};
        inputvalues = filelines[i].split(','); 

            Account a = new Account();
            a.Name = inputvalues[0]; 
            a.Phone = input values[1]; 
            a.Shipping Street = inputvalues[2];       
            a.ShippingCity = inputvalues[3];
            a.ShippingState = inputvalues[4];
            a.ShippingPostalCode = inputvalues[5];
            a.ShippingCountry = inputvalues[6];
            System.Debug('Account Entry'+contoupload.size());

       insert contoupload;
    catch (Exception e)
        ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
     PageReference pg = new PageReference('https://csesparks-dev-ed--pack-saleforce.ap1.visual.force.com/apex/Fileupload');
     return pg;

Error like this:

System.ListException: List index out of bounds: 2

  • 1
    Can you post an example of your CSV file? On the face of it, it appears simply because your inputvalues array is reflecting the true number of columns in your CSV. Where you expecting it to have 5? Commented Apr 15, 2013 at 14:51
  • 1
    Also, depending on the source of your CSV file or the likelyhood of quotes in the data values etc, you may want to consider enhancing your CSV parsing logic a little. Or utilise the library here. frombelvideres4thfloor.blogspot.com.es/2010/10/… Commented Apr 15, 2013 at 14:53
  • Thanks for your comment Andrew Fawcett...I am having a csv file column as Name,Phone and in apex class i added 5 columns.As my point of view is it possible to do like added 2 or 3 columns in excel sheet and add 5array values in apex class?And it is add in to the SObject or any other way to do this program........ Commented Apr 16, 2013 at 6:09

1 Answer 1


Given your current code, the number of columns in your file needs to match the number of array items your code is expecting in the 'inputvalues' array.

Looking at the code you have provided, you need to provide 7 columns on each row in the file. If you don't, you will get the List index out of bounds error.

So you cannot just provide the Name and Phone in the CSV file, you need to provide...

Fred Blogs,+44123456789,Fred Street,Fred City,Fred State,Fred Postalcode,Fred Country

Or enhance your code to check 'inputvalues.size()' before accessing an array item.

If you want to do something more dynamic perhaps include the API name as a CSV header row.

Fred Blogs,+44123456789,Fred Street,Fred City,Fred State,Fred Postalcode,Fred Country
Fred Blogs,+44123456789,Fred Street,Fred City,Fred State,Fred Postalcode,Fred Country
Fred Blogs,+44123456789,Fred Street,Fred City,Fred State,Fred Postalcode,Fred Country

Read the first row and retain the field names, then iterate over these for each subsequent row using the SObject.put method to set the field value using to the 'inputvalues' array item.

  • AndrewFawcett Thanks for your comment....Is any other way to do adding two or more rows in excel sheet and add the csv records to Sobjects without mention the field names.By default whatever fields i load in to csv file and automatically inserted in to SObjects using apex class.... Commented Apr 18, 2013 at 6:44
  • Yes you can use the SObject.put method to dynamically assign values from the CSV columns. Your CSV header should then probably contain the API name of the field. Commented Apr 18, 2013 at 6:53
  • AndrewFawcett, please provide any examples like this SObject.put method because am new to salesforce...... Commented Apr 18, 2013 at 10:05
  • Please review this section of the Apex documentation on dynamic DML, salesforce.com/us/developer/docs/apexcode/Content/…. Let me know if this helps answer your original question. Commented Apr 18, 2013 at 10:22
  • Now i got idea from this dynamic DML...Thanks for your comment....Andrew Fawcett.......... Commented Apr 18, 2013 at 13:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .