0

Can any one help me out with this Error ??I have records created in the object.Any help is much appreciated

Visualforce Error

System.ListException: List index out of bounds: 0 
Class.QuoteContentController.<init>: line 234, column 1

This is the Error line

QtyGrndTtl[j] = Qrtr1QtyGrndTtl + Qrtr2QtyGrndTtl + Qrtr3QtyGrndTtl + Qrtr4QtyGrndTtl;

Code :

    public with sharing class QuoteContentController {
    public Map < String, Decimal > PartMap {get;set;}
    public Map < string, Decimal > Quarter1 {get;set;}  
    public Map < string, Decimal > Quarter2 {get;set;}  
    public Map < string, Decimal > Quarter3 {get;set;}  
    public Map < string, Decimal > Quarter4 {get;set;}  
    public Map < string, Decimal > Amount1 {get;set;}  
    public Map < string, Decimal > Amount2 {get;set;}  
    public Map < string, Decimal > Amount3 {get;set;}  
    public Map < string, Decimal > Amount4 {get;set;}  
    public Competitor__c com {get;set;}  
    public gmod__Opportunity_Forecast__c opflist {get;set;}  
    public Id qId {get;set;}  
    Public string all {get;set;}  

    Public QuoteContentController() {}
    Public QuoteContentController(ApexPages.StandardController controller) {
        qId = Apexpages.currentPage().getparameters().get('Id');
    }
        Public Integer subtotalofquantity{get;set;}    

        Public Integer subtotalofamount{get;set;}

        Public Integer Colquarter1subtotal{get;set;}  
        Public Integer quarter2subtotal{get;set;}  

        Public integer Qrtr1QtyGrndTtl =  0;
        Public integer Qrtr2QtyGrndTtl =  0;
        Public integer Qrtr3QtyGrndTtl =  0;
        Public integer Qrtr4QtyGrndTtl =  0;
        list<integer>QtyGrndTtl = new list<integer>();

        Public decimal Qrtr1AmtGrndTtl =  0;
        Public decimal Qrtr2AmtGrndTtl =  0;
        Public decimal Qrtr3AmtGrndTtl =  0;
        Public decimal Qrtr4AmtGrndTtl =  0;
        list<decimal>AmtGrndTtl = new list<decimal>();  



        Public List<wrapperClass> disp_list {get;set;}{
        subtotalofquantity = 0;
        subtotalofamount = 0;
        Colquarter1subtotal = 0;
        quarter2subtotal = 0;

        integer Qrtr1QtyGrndTtl =  0;
        integer Qrtr2QtyGrndTtl =  0;
        integer Qrtr3QtyGrndTtl =  0;
        integer Qrtr4QtyGrndTtl =  0;


       decimal Qrtr1AmtGrndTtl =  0;
       decimal Qrtr2AmtGrndTtl =  0;
       decimal Qrtr3AmtGrndTtl =  0;
       decimal Qrtr4AmtGrndTtl =  0;




        disp_list = new list < wrapperclass > ();
        //Query all the list     
        List < Quote > q = [select id, Name, QuoteNumber, Effective_Date__c, Comments__c, Quote.Opportunity.id,
                                    Quote.Opportunity.Probability, Quote.Opportunity.AccSegment__c, 
                                    Quote.Opportunity.AccApplication__c, Quote.Opportunity.Persona__c, Quote.Opportunity.Region__c
                                    from Quote where id = : apexpages.currentpage().getparameters().get('id')];
        Opportunity opp = [select id, Name, (select id, Quantity, product2id from OpportunityLineItems), 
                                probability, AccSegment__c 
                                from Opportunity where opportunity.Id = : q[0].opportunity.id LIMIT 1];
        List < gmod__Opportunity_Forecast__c > opflist = [
                                Select id, Name, gmod__opportunity__r.id, gmod__Quantity__c, gmod__Price__c,
                                gmod__Month__c, gmod__date__c, gmod__Quarter__c, gmod__Amount__c, Actual_Price__c, 
                                gmod__Year__c, gmod__Month_Text__c, Forecast_Date__c, gmod__Product__r.Name, 
                                gmod__opportunity__r.name from gmod__Opportunity_Forecast__c 
                                WHERE gmod__Product__c != null and gmod__opportunity__r.id = : opp.id 
                                Order BY gmod__Year__c, gmod__Month__c asc];
        //Iterate through each list to extract the values and add it to the custom wrapper data type  

        PartMap = new Map < String, Decimal > ();
        Quarter1 = new Map < String, Decimal > ();
        Quarter2 = new Map < String, Decimal > ();
        Quarter3 = new Map < String, Decimal > ();
        Quarter4 = new Map < String, Decimal > ();
        Amount1 = new Map < String, Decimal > ();
        Amount2 = new Map < String, Decimal > ();
        Amount3 = new Map < String, Decimal > ();
        Amount4 = new Map < String, Decimal > ();

        for (gmod__Opportunity_Forecast__c oppforecast: opflist) {

            String prodName = oppforecast.gmod__Product__r.Name;
            String monthText = oppforecast.gmod__Month_Text__c;

            Map<String, Decimal> quarterMap;
            Map<String, Decimal> amountMap;

            if (monthText == 'Jan' || monthText == 'Feb' || monthText == 'Mar'){
                quarterMap = Quarter1;
                amountMap = Amount1;
            if (quarterMap.containskey(prodName)) {
                quarterMap.put(prodName, quarterMap.get(prodName) + oppforecast.gmod__Quantity__c);
                amountMap.put(prodName, amountMap.get(prodName) + oppforecast.gmod__Amount__c);
            } else {
                quarterMap.put(prodName, oppforecast.gmod__Quantity__c);
                amountMap.put(prodName, oppforecast.gmod__Amount__c);
                wrapperclass w = new wrapperclass();
            w.gmod_Opportunity = oppforecast.gmod__Opportunity__r.Name;
            w.gmod_Product = prodName;
            w.gmod_Quantity = oppforecast.gmod__Quantity__c;
            w.gmod_Price = oppforecast.gmod__Price__c;
            w.Name = oppforecast.Name;
            w.gmod_Quarter = oppforecast.gmod__Quarter__c;
            w.gmod_Month = oppforecast.gmod__Month__c;
            w.gmod_Amount = oppforecast.gmod__Amount__c;
            w.Actual_Price = oppforecast.Actual_Price__c;
            w.gmod_Year = oppforecast.gmod__Year__c;
            w.gmod_date = oppforecast.gmod__date__c;
            w.gmod_Month_Text = monthText;
            w.Forecast_Date = oppforecast.Forecast_Date__c;
            disp_list.add(w);
        }
       }         
            if (monthText == 'Apr' || monthText == 'May' || monthText == 'June'){
                quarterMap = Quarter2;
                amountMap = Amount2;
                if (quarterMap.containskey(prodName)) {
                quarterMap.put(prodName, quarterMap.get(prodName) + oppforecast.gmod__Quantity__c);
                amountMap.put(prodName, amountMap.get(prodName) + oppforecast.gmod__Amount__c);
            } else {
                quarterMap.put(prodName, oppforecast.gmod__Quantity__c);
                amountMap.put(prodName, oppforecast.gmod__Amount__c);

                }
                }
            if (monthText == 'Jul' || monthText == 'Aug' || monthText == 'Sept'){
                quarterMap = Quarter3;
                amountMap = Amount3;
                if (quarterMap.containskey(prodName)) {
                quarterMap.put(prodName, quarterMap.get(prodName) + oppforecast.gmod__Quantity__c);
                amountMap.put(prodName, amountMap.get(prodName) + oppforecast.gmod__Amount__c);
            } else {
                quarterMap.put(prodName, oppforecast.gmod__Quantity__c);
                amountMap.put(prodName, oppforecast.gmod__Amount__c);
            }
            }
            if (monthText == 'Oct' || monthText == 'Nov' || monthText == 'Dec') {
                quarterMap = Quarter4;
                amountMap = Amount4;
                 if (quarterMap.containskey(prodName)) {
                quarterMap.put(prodName, quarterMap.get(prodName) + oppforecast.gmod__Quantity__c);
                amountMap.put(prodName, amountMap.get(prodName) + oppforecast.gmod__Amount__c);
            } else {
                quarterMap.put(prodName, oppforecast.gmod__Quantity__c);
                amountMap.put(prodName, oppforecast.gmod__Amount__c);


          /*  wrapperclass w = new wrapperclass();
            w.gmod_Opportunity = oppforecast.gmod__Opportunity__r.Name;
            w.gmod_Product = prodName;
            w.gmod_Quantity = oppforecast.gmod__Quantity__c;
            w.gmod_Price = oppforecast.gmod__Price__c;
            w.Name = oppforecast.Name;
            w.gmod_Quarter = oppforecast.gmod__Quarter__c;
            w.gmod_Month = oppforecast.gmod__Month__c;
            w.gmod_Amount = oppforecast.gmod__Amount__c;
            w.Actual_Price = oppforecast.Actual_Price__c;
            w.gmod_Year = oppforecast.gmod__Year__c;
            w.gmod_date = oppforecast.gmod__date__c;
            w.gmod_Month_Text = monthText;
            w.Forecast_Date = oppforecast.Forecast_Date__c;
            disp_list.add(w);*/
        }
}
            for (Quote qt: q) {
            System.debug('Quote Size ++ '+q.size());
             System.debug('opp forcast ++ ' +opflist.size());
                for (integer i = 0; i < opflist.size(); i++) {
               // if(Quarter1)
                    subtotalofquantity += integer.valueOf(opflist[i].gmod__Quantity__c);
                    System.debug('subtotalofquantity*%%%%%%%%% ++ ' +subtotalofquantity);
                    subtotalofamount += integer.valueOf(opflist[i].gmod__Amount__c);
                    System.debug('subtotalofamount*************** ++ ' +subtotalofamount);
                    //subtotalofamount =tempsubtotalofamount;
                    //Integer Colquarter1subtotalTemp = integer.valueOf(opflist[i].gmod__Quantity__c);
                    // Colquarter1subtotal += Colquarter1subtotalTemp;
                   //  System.debug('Colquarter1*************** ++ ' +Colquarter1subtotal);
                   // Integer quarter2subtotalTemp = Integer.valueOf(opflist[i].gmod__Quantity__c);
                   // quarter2subtotal += quarter1subtotalTemp;

                    //Instantiating the wrapper SObject     
                    wrapperclass w = new wrapperclass();
                    //Assigning the wrapper variables from the SObject Fields in the database.     
                    w.gmod_Opportunity = opflist[i].gmod__Opportunity__r.Name;
                    w.gmod_Product = opflist[i].gmod__Product__r.Name;
                    w.gmod_Quantity = opflist[i].gmod__Quantity__c;
                    w.gmod_Price = opflist[i].gmod__Price__c;
                    w.Name = opflist[i].Name;
                    w.gmod_Quarter = opflist[i].gmod__Quarter__c;
                    w.gmod_Month = opflist[i].gmod__Month__c;
                    w.gmod_Amount = opflist[i].gmod__Amount__c;
                    w.Actual_Price = opflist[i].Actual_Price__c;
                    w.gmod_Year = opflist[i].gmod__Year__c;
                    w.gmod_date = opflist[i].gmod__date__c;
                    w.gmod_Month_Text = opflist[i].gmod__Month_Text__c;
                    w.Forecast_Date = opflist[i].Forecast_Date__c;
                }//End of For loop int
            }//End of For loop Quote
        }//End of For loop gmod
        for(gmod__Opportunity_Forecast__c oppforecast1 : opflist) {
         integer j = 0;

    for(j=0; j<3; j++){

    // replace oppforecast.gmod__Product__r.Name[i] with the product name string
    Integer Qty01 = Integer.valueOf(Quarter1.get(oppforecast1.gmod__Product__r.Name));
    Integer Qty02 = Integer.valueOf(Quarter2.get(oppforecast1.gmod__Product__r.Name));
    Integer Qty03 = Integer.valueOf(Quarter3.get(oppforecast1.gmod__Product__r.Name));
    Integer Qty04 = Integer.valueOf(Quarter4.get(oppforecast1.gmod__Product__r.Name));
    Integer Qty11 = Integer.valueOf(Quarter1.get(oppforecast1.gmod__Product__r.Name));
    Integer Qty12 = Integer.valueOf(Quarter2.get(oppforecast1.gmod__Product__r.Name));
    Integer Qty13 = Integer.valueOf(Quarter3.get(oppforecast1.gmod__Product__r.Name));
    Integer Qty14 = Integer.valueOf(Quarter4.get(oppforecast1.gmod__Product__r.Name));
    Integer Qty21 = Integer.valueOf(Quarter1.get(oppforecast1.gmod__Product__r.Name));
    Integer Qty22 = Integer.valueOf(Quarter2.get(oppforecast1.gmod__Product__r.Name));
    Integer Qty23 = Integer.valueOf(Quarter3.get(oppforecast1.gmod__Product__r.Name));
    Integer Qty24 = Integer.valueOf(Quarter4.get(oppforecast1.gmod__Product__r.Name));

    System.debug('qty@@@@@@@@@@@@@123 ++ ' + QtyGrndTtl);

    Qrtr1QtyGrndTtl = 0 + Qty01 + Qty11 + Qty21; //reset to zero each yr.
    Qrtr2QtyGrndTtl = 0 + Qty02 + Qty12 + Qty22;
    Qrtr3QtyGrndTtl = 0 + Qty03 + Qty13 + Qty23;
    Qrtr4QtyGrndTtl = 0 + Qty04 + Qty14 + Qty24;
    QtyGrndTtl[j] = Qrtr1QtyGrndTtl + Qrtr2QtyGrndTtl + Qrtr3QtyGrndTtl + Qrtr4QtyGrndTtl;

     System.debug('qty@@@@@@@@@@@@@ ++ ' + QtyGrndTtl);



    // replace oppforecast.gmod__Product__r.Name[i] with the product name string
    decimal amt01 = Amount1.get(oppforecast1.gmod__Product__r.Name);
    decimal amt02 = Amount2.get(oppforecast1.gmod__Product__r.Name);
    decimal amt03 = Amount3.get(oppforecast1.gmod__Product__r.Name);
    decimal amt04 = Amount4.get(oppforecast1.gmod__Product__r.Name);
    decimal amt11 = Amount1.get(oppforecast1.gmod__Product__r.Name);
    decimal amt12 = Amount2.get(oppforecast1.gmod__Product__r.Name);
    decimal amt13 = Amount3.get(oppforecast1.gmod__Product__r.Name);
    decimal amt14 = Amount4.get(oppforecast1.gmod__Product__r.Name);
    decimal amt21 = Amount1.get(oppforecast1.gmod__Product__r.Name);
    decimal amt22 = Amount2.get(oppforecast1.gmod__Product__r.Name);
    decimal amt23 = Amount3.get(oppforecast1.gmod__Product__r.Name);
    decimal amt24 = Amount4.get(oppforecast1.gmod__Product__r.Name);

    Qrtr1AmtGrndTtl = 0 + amt01 + amt11 + amt21; //reset to zero each yr.
    Qrtr2AmtGrndTtl = 0 + amt02 + amt12 + amt22;
    Qrtr3AmtGrndTtl = 0 + amt03 + amt13 + amt23;
    Qrtr4AmtGrndTtl = 0 + amt04 + amt14 + amt24;
    AmtGrndTtl[j] = Qrtr1AmtGrndTtl + Qrtr2AmtGrndTtl + Qrtr3AmtGrndTtl + Qrtr4AmtGrndTtl;

     System.debug('amt############# ++ ' +AmtGrndTtl);

    j++;

}

Decimal GrndAmtGrndTtl = AmtGrndTtl[0] + AmtGrndTtl[1] + AmtGrndTtl[2];
Integer GrndQtyGrndTtl = QtyGrndTtl[0] + QtyGrndTtl[1] + QtyGrndTtl[2]; 

        }
    }//End of disp


    //Declare a wrapper class      
    public class Wrapperclass  {
        //custom wrapper datatype      
        Public string Name{get;set;}  
        Public string AccountType{get;set;}  
        Public date todaysDate{get;set;}  
        Public date Expected_Order_Date{get;set;}
        Public string Probability{get;set;}  
        Public string Internal_Comment{get;set;}  
        Public string External_Comment{get;set;}      
        Public string Segment{get;set;}  
        Public string Application{get;set;}  
        Public string Persona{get;set;}  
        Public string Geogrpahy{get;set;}      
        Public string PartNumbers{get;set;}  
        Public Decimal  Price{get;set;}  
        Public Decimal End_Customer_Price{get;set;}  
        Public Decimal Quantity {get;set;}  
        Public Decimal Total{get;set;}      
        Public string RFQ_justification{get;set;}  
        Public string Main_Customer_of_Account{get;set;}  
        Public string Bridgelux_competition_at_account{get;set;}
        Public string Geographic_regions_serviced{get;set;}  
        Public string Annual_lighting_revenue{get;set;}  
        Public string Annual_LED_revenue_or_percent{get;set;}  
        Public string Annual_purchases_of_LED_light_sources{get;set;}
        Public string Percent_of_LED_purchases_that_are_COB{get;set;}  
        Public string Other_information{get;set;}      
        Public string Product_Series{get;set;}  
        Public string Volume{get;set;} 
        Public string Date_Price_is_Valid{get;set;}     
        Public string gmod_Opportunity{get;set;}
        Public string gmod_Product{get;set;}
        Public Decimal gmod_Quantity{get;set;}
        Public Decimal gmod_Price{get;set;}
        Public Decimal gmod_Quarter{get;set;}
        Public Decimal gmod_Month{get;set;}
        Public Decimal gmod_Amount{get;set;}
        Public Decimal Actual_Price{get;set;}
        Public Decimal  gmod_Year{get;set;}
        Public Date gmod_date{get;set;}
        Public string gmod_Month_Text{get;set;}
        Public Date  Forecast_Date{get;set;}
    }
  //  }
  • QtyGrndTtl has no elements so when j = 0 you get that error. Can you show the part of your code where you initialize QtyGrndTtl? – BarCotter Mar 30 '15 at 11:25
  • @ BarCotter:I have update the code .Any help very much appreciated. – nikkey Mar 30 '15 at 11:39
1

Empty lists do not function the same as empty arrays.

An empty list must be added to using the add() method rather than assigning indexes directly. You can then use the array index style notation to directly modify the values that you have added to the list.

List<Integer> ints = new List<Integer>();
ints.add(7); // ints is now { 7 }
ints.add(3); // ints is now { 7, 3 }

ints[0] = 1; // ints is now { 1, 3 }
ints[1] = 1; // ints is now { 1, 1 }
ints[2] = 0; // List index out of bounds: 2

When using fixed sized arrays you can declare them to be a certain size and then assign to any element as you please at that point.

Integer[] ints = Integer[2]; // ints is now { null, null }

ints[1] = 1; // ints is now { null, 1 }
ints[0] = 7; // ints is now { 7, 1 }
ints[2] = 0; // List index out of bounds: 2

To fix your issue you can either change the line you mentioned to use the add() method:

QtyGrndTtl.add(Qrtr1QtyGrndTtl + Qrtr2QtyGrndTtl + Qrtr3QtyGrndTtl + Qrtr4QtyGrndTtl);

Or, since you know you'll always have 3 values you can switch to using a fixed sized array to store them instead, which lets you keep your assignment the way it is:

Integer[] QtyGrndTtl = new Integer[3];
  • :The array size is not fixed.For an example im using the size.But still tried it throws an Error at same Error at line{ Decimal GrndAmtGrndTtl = AmtGrndTtl[0] + AmtGrndTtl[1] + AmtGrndTtl[2];}.When i changed the Decimal to integer ,it throws another error as illegal assignment from decimal to integer.Can u let me know how to use the add method.Any help very much appreciated. – nikkey Mar 30 '15 at 12:18

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.