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
Shivani DesaiShivani Desai 

Test Class giving List Index Out Of Bounds Error

Hi,

I am constantly getting this error in a test class no matter how many changes I make or how many times I try to get the current page Id. Guess am having trouble writing the correct syntax or order. Any help will be greatly appreciated. My visualforce page is creating a record and then creating or updating child records for the record created using csv upload.
This is my controller code 
public with sharing class FileUploader1
{
  
    private final Sobject parent;
    public string nameFile{get; set;}
    public Blob contentFile{get;set;}
    //public String theId;
    public List<gii__InventoryAdjustment__c> adj;
    public List<gii__ProductSerial__c> accstoupload;
    String[] filelines = new String[]{};
    private ApexPages.standardController standardController;
   
    public String theId = ApexPages.currentPage().getParameters().get('id');
   
    public FileUploader1(ApexPages.StandardController stdcontroller) {
          }
   
  //public FileUploader1(string n, blob c) {
    //this.namefile = n;
    //this.contentfile = c;
    //}
       
    public Pagereference ReadFile()
    {           
        nameFile=contentFile.toString();
        filelines = nameFile.split('\n');
       
       
       
        accstoupload = new List<gii__ProductSerial__c>();
        Integer size = filelines.size()-1;
       
        gii__InventoryAdjustment__c []adj = [SELECT gii__Warehouse__c,gii__Product__c,gii__ProductInventory__c FROM gii__InventoryAdjustment__c WHERE Id = :TheId];
      

       
        for (Integer i=1;i<filelines.size();i++)
        {
           
            String[] inputvalues = filelines[i].split(',');
            String name = inputvalues[0].trim();
            String Carton = inputvalues[1].trim();
            String Pallet = inputvalues[2].trim();
            String MBSerial = inputvalues[3].trim();
            string Mydate = inputvalues[4].trim();
            string Model = inputvalues[5].trim();
            String Refurbished = inputvalues[6].trim();
            //string school = inputvalues[7].trim();
            string price = inputvalues[7].trim();
           
                     
           
           
                       
            gii__ProductSerial__c a = new gii__ProductSerial__c();
            a.Name = name;
            a.CARTON_NO__c = Carton;
            a.PALLET_NO__c= Pallet;
            a.MBSERIAL_Number__c = MBSerial;
            a.Manufacturer_Date__c = date.parse(Mydate);
            a.Model__c = model;
            a.Refurbished__c = Refurbished;
            a.Price__c = price;
            a.gii__InventoryAdjustment__c = adj[0].Id;
            a.gii__Product__c = adj[0].gii__Product__c;
            a.gii__ProductInventory__c = adj[0].gii__ProductInventory__c;
            a.gii__Warehouse__c = adj[0].gii__Warehouse__c;
            a.gii__TransactionType__c = 'Inventory Adjustment';
          //  a.School_Registered_At__c = school;
           
            accstoupload.add(a);
           
        }
        try{
        upsert accstoupload MBSERIAL_Number__c ;
        }
         catch (Exception e)
        {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
            ApexPages.addMessage(errormsg);
        }
     
       
       
    return null;
    }
  
}

And my test class is as follows
@isTest(SeeAllData = true)
private class FileUploader1Test {

    public static testmethod void constructorTest() {
   
    gii__InventoryAdjustment__c adj = new gii__InventoryAdjustment__c();
    adj.gii__AdjustmentQuantity__c = 20;
      adj.gii__AdjustmentDate__c = System.today();
      adj.gii__ProductInventory__c = 'a1c11000000AJM7';
      adj.gii__Reason__c = 'Inventory Adjustment';
      insert adj;

      PageReference pgRef = Page.Inventory_Adjustment_Another;
      //use the page reference Apex Class to instantiate a page named Inventory_Adjustment_Another
    
      Test.setCurrentPage(pgRef);
      //set the filled values for adj
     
      ApexPages.StandardController thecontroller = new ApexPages.StandardController(adj);
      //Initiation of the controller
     
      FileUploader1 controller = new FileUploader1(thecontroller);
      //the controller
    
      system.assert(thecontroller != null);
     
      thecontroller.save();
      String TheId = ApexPages.currentPage().getParameters().put('ID',adj.id);
       //blob forcontent = blob.valueof(blobcreator);
               
       String blobCreator =  '90JV1300002H42100650,G642109162,P452805539,EG451507895,6/15/2014,CD3,No,199' + '\r\n' +
                                '90JV1300002H42100651,G642109193,P452805539,EG451507643,6/15/2014,CD3,No,199'+ '\r\n' +
                                '90JV1300002H42100652,G642109162,P452805539,EG451507672,6/15/2014,CD3,No,199' + '\r\n' +
                                '90JV1300002H42100653,G642109161,P452805539,EG451507677,6/15/2014,CD3,No,199';
        
        blob forcontent = blob.valueof(blobcreator);
        controller.contentFile = forcontent;
        controller.nameFile = blobCreator;
        ApexPages.currentPage().getParameters().put('id', adj.id);
       
        Controller.ReadFile();
              
      
      }
}

this is the error that I am recieving" List index out of bounds: 0". Greatly appreciate any help possible.


Best Answer chosen by Shivani Desai
Jen BennettJen Bennett
gp is the parameter and yyyy is the value they are sending, it's just test data but where you need an id sent in they need gp sent in. So if you look at the example were they are setting the required parameter, like in your case the id, they set it before instantiating the controller a 2nd time. Try setting the parameter before instiating the controller (ApexPages.currentPage().getParameters().put('id', adj.id);)

All Answers

Jen BennettJen Bennett
Looks like you need to set the parameter before instantiating the controller. The example shown here, tells you how to test with and without a required parameter:
http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm
Shivani DesaiShivani Desai
Hi jjbennett,

Thank you for the quick response. Am a little new to apex . Could you please elaborate a little or point where exactly? I referred the article provided by you but am unable to understand what 'qp' and 'yyyy' stand for. Are they setting the id of the record?
Jen BennettJen Bennett
gp is the parameter and yyyy is the value they are sending, it's just test data but where you need an id sent in they need gp sent in. So if you look at the example were they are setting the required parameter, like in your case the id, they set it before instantiating the controller a 2nd time. Try setting the parameter before instiating the controller (ApexPages.currentPage().getParameters().put('id', adj.id);)
This was selected as the best answer
Shivani DesaiShivani Desai
Can I set the parameter in this manner 
controller.adj = [SELECT Id,gii__Warehouse__c,gii__Product__c,gii__ProductInventory__c FROM gii__InventoryAdjustment__c LIMIT 1 ];

I tried doing it and it is still failing? Am very thankful to you for all the help.

Shivani DesaiShivani Desai
Thanks JJbennett! It worked out.