1

enter image description hereCan any one help me out with this Error ??Any help is much appreciated

Error in VisualForce Page as :List index out of bounds: 0

Here is my VF Page and the Class.

Visual Force Page :

<apex:page standardController="Opportunity" renderAs="pdf" applyhtmltag="false" showHeader="false" applyBodyTag="false" extensions="InvoiceController">
    <head>
        <style>
        @page {
            margin: 1px 50px 225px 50px; 
            size:A4;
            Padding-top:30px;

            @top-center { 
                    content : element(header);
                 }

            @bottom-center {
                    content : element(footer);
                    height:200px;
                    padding-top:10px;
                 }
        } 
        body { font-family: 'Arial Unicode MS'; }
        .companyName { font: bold 30px; color: red; }  
        table {width:100%; border-spacing:0; border-collapse:collapse; border-width: 1px; border-color:#C0C0C0;border-style:solid;} 
        .header-table-data { border-width: 1px; border-color:Black;border-style:solid;background-color:#FFFFFF;
                             color:Black; font-size:14px; font-family:Verdana;
                             text-align:center;}
        .header-table-heading {border-width: 1px; border-color:Black;border-style:solid;background-color:#C0C0C0; 
                               font-weight:bold;color:Black; font-size:14px; font-family:Verdana;
                               text-align:center;}
        .header-data { border-width: 0px; border-color:Black;border-style:solid;background-color:#FFFFFF;
                       color:Black; font-size:14px; font-family:Verdana;text-align:left; padding-right: 10px;}

        .header-heading {border-width: 0px; border-color:Black;border-style:solid; 
                         font-weight:bold;color:Black; font-size:14px; font-family:Verdana;
                         text-align:left;}
    </style>

</head>
<body> 

    <table>
        <tr>
            <td><img style="WIDTH:25%; HEIGHT: 25%" src='{!URLFOR($Resource.Bridgelux_New_Logo)}' />
            </td>
        </tr>
        <br/>
        <tr>
            <td><b>Commercial Invoice</b>
            </td>
        </tr>
    </table><br/>

    <table cellspacing="0" cellpadding="0" width="100%" border="0" >
        <tr>
            <td border="0" bgcolor="Silver">Sold To</td>
            <td bgcolor="Silver">Ship To:</td>
            <td bgcolor="Silver">Ship From</td>
        </tr>
        <tr>
            <td style="border-width: 0px; border-color:Black;border-style:solid;background-color:#FFFFFF;" align="left" width="10%">
                <f style="color:Black; font-size:12px; font-family:Verdana;">
                    <!-- Code starts here ********************************************************* -->
                    <!-- {!showaddressonVf} -->
                    <apex:outputText value="{!opportunity.account.Name}"></apex:outputText><br/>
                    <!-- Added by Sumit -->
                    <apex:outputText value=" {!all}" escape="false"></apex:outputText>
                    <!--     -----------   -->
                    <br />
                </f>
            </td>      
            <td style="border-width: 0px; border-color:Black;border-style:solid;background-color:#FFFFFF;" align="left" width="10%">
            <f style="color:Black; font-size:12px; font-family:Verdana;">
                <!-- Code starts here ********************************************************* -->
                <!-- Added below line by KiranmaiKosaraju. To show Ship To Account Name If diff from Account Name or else show Account Name -->
                <apex:outputText value="{!IF ((opportunity.Ship_To_Account_Name__c = NULL),  opportunity.account.Name,opportunity.Ship_To_Account_Name__c)}"></apex:outputText><br /> 
                <!--<apex:outputText value="{!opportunity.account.Name}"></apex:outputText><br /> -->
                <!-- Added by Sumit -->
                <apex:outputText value=" {!all}" escape="false"></apex:outputText>
                <!--     -----------   -->
                <br />  
            </f>
            </td>
            <td style="border-width: 0px; border-color:Black;border-style:solid;background-color:#FFFFFF;" align="left" width="10%">
            <f style="color:Black; font-size:12px; font-family:Verdana;">
                Bridgelux Inc. C/O JSI Logistics (HK) Ltd.<br/>
                16/F Tsuen Wan International Center,<br/>
                68 Wang Lung Street, Tsuen Wan,<br/>
                Hong Kong<br/>
                Tel: (852) 3761 7080<br/>
                Fax: (852) 2750 0721<br/>
            </f>
            <f style="color:Blue; font-size:12px; font-family:Tahoma;"> Email:Info@bridgelux.com</f>
            <f style="color:Blue; font-size:10px; font-family:Trebuchet MS;"><br/> </f>
            </td>
        </tr>
    </table> 

     <table border="0" cellspacing="0" cellpadding="1" width="100%" >
        <tr>
             <td class="header-heading">
                Ship Date:
             </td>
             <td class="header-data">
                <apex:outputText value="{!opportunity.Sys_today_s_date__c}"></apex:outputText><br/>
             </td>
             <td class="header-heading">
                Special Instructions:
             </td>
             <td class="header-data">                
                <apex:outputText value="{!opportunity.Special_Instructions__c}"></apex:outputText>
             </td>
        </tr>
    </table> 

    <table>
        <tr border="0">
            <td class="header-table-heading">CI Invoice Id</td>
            <td class="header-table-heading">Ship Date<b/></td>
            <td class="header-table-heading">Sales Order#</td>
            <td class="header-table-heading">Order Date</td>
            <td class="header-table-heading">Customer#</td>
        </tr>
        <tr>
            <td class="header-table-data"><apex:outputText value="{!Opportunity.Salesorder_number__c}"></apex:outputText></td>
            <td class="header-table-data"><apex:outputText value="{!opportunity.Sys_today_s_date__c}"></apex:outputText></td>
            <td class="header-table-data"><apex:outputText value="{!Opportunity.Salesorder_number__c}"></apex:outputText></td>
            <td class="header-table-data"><apex:outputText value="{!Opportunity.Sys_createddate__c}"></apex:outputText></td>
            <td class="header-table-data">{!Opportunity.account.Ebs_Customer_number__c}</td>
        </tr>
    </table>
    <br/>
    <table >
        <tr>
            <td class="header-table-heading">LN</td>
            <td class="header-table-heading">Item</td>
            <td class="header-table-heading">Customer Item #<b/></td>
            <td class="header-table-heading">COO</td>
            <td class="header-table-heading">ECCN #</td>
            <td class="header-table-heading">UOM</td>
            <td class="header-table-heading">Unit Price</td>
            <td class="header-table-heading">Quantity</td>
            <td class="header-table-heading">Extension</td>
        </tr>
        <tr>
            <td class="header-table-heading"></td>
            <td colspan="8" style="text-align:left"  class="header-table-heading">Description</td>
        </tr>
        <apex:repeat value="{!appointmentList}" var="line">
        <apex:outputPanel layout="none" rendered="{!if(line.pick==true, true, false)}">

        <tr>         
            <td class="header-table-data"><apex:outputText value="{!line.LineNumber}"></apex:outputText></td>
            <td class="header-table-data"><apex:outputText value="{!line.productname}"></apex:outputText></td>
            <td class="header-table-data"></td>
            <td class="header-table-data"></td>
            <td class="header-table-data">8541.40.2000</td>
            <td class="header-table-data">EA</td>
            <td class="header-table-data">$1</td>
            <td class="header-table-data"><apex:outputtext value="{!line.quantity}"></apex:outputtext></td>
            <td class="header-table-data"><apex:outputText value="{!line.extension}"></apex:outputText></td>
        </tr>
        <tr>
            <td class="header-table-data"></td>

            <td colspan="8" style="text-align:left" class="header-table-data"><b>Scheduled Ship Date:</b>
                        <apex:outputText value="{0,date,MM'/'dd'/'yyyy}">
                                <apex:param value="{!line.Scheduledshipdate}"/>
                        </apex:outputText> </td>
        </tr>
        </apex:outputPanel>    
        </apex:repeat>
    </table>
    <br/>
    <table>
        <tr>
            <td class="header-table-heading">Gross Weight</td>
            <td class="header-table-heading">Net Weight</td>
            <td class="header-table-heading">Shipped Via</td>
            <td class="header-table-heading">Tracking #</td>
            <td class="header-table-heading">Payment Terms</td>
            <td class="header-table-heading">Incoterms </td>
            <td class="header-table-heading">Currency </td>
            <td rowspan="0" class="header-table-heading">Sub Total <br/>Shipping Charges</td>
            <td rowspan="0" class="header-table-data"><apex:outputText value="{!opportunity.Pick_Total__c}"></apex:outputText>
                 <br/><apex:outputText value="{!Opportunity.Shipping_Charges__c}"></apex:outputText><br/></td>

        </tr>
        <apex:repeat value="{!appointmentList1}" var="line1">
        </apex:repeat>
        <tr>
            <td class="header-table-data"><apex:outputText value="{!Opportunity.Gross_Weight__c}"></apex:outputText></td>
            <td class="header-table-data"><apex:outputText value="{!Opportunity.Net_weight__c}"></apex:outputText></td>
            <td class="header-table-data"><apex:outputText value="{!Opportunity.Shipped_Via__c}"></apex:outputText></td>
            <td class="header-table-data"><apex:outputText value="{!Opportunity.Bill_of_Lading__c}"> </apex:outputText></td>
            <td class="header-table-data">Net 30</td>
            <td class="header-table-data">EXW</td>
            <td class="header-table-data">USD</td>
            <td rowspan="0" class="header-table-heading"> Total Amount Due</td>
            <td class="header-table-data"><apex:outputText value="{!opportunity.Pick_Total__c + opportunity.Shipping_Charges__c}"></apex:outputText></td>
        </tr>
    </table>
</body>
</apex:page>

My Controller Class :

public with sharing class InvoiceController {
    List<wrapperClass> appointmentList {get; set;}

    List<wrapperClass> appointmentList1 {get; set;}
    public list<string> addaress {get; set;}
    public string address1 {get; set;}
    public string address2 {get; set;}
    public string address3 {get; set;}
    public string address4 {get; set;}
    public string address5 {get; set;}
    public string address6 {get; set;}
    public string address7 {get; set;}
    public  String showaddressonVf {get; set;}
    public string wholeaddress {get; set;} //Added by sumit
    List<String> stringList;
    public String all {get; set;}
    //------------
    public InvoiceController() {
        list<opportunity> op = [select id, Shipment_Info__c   from opportunity where id = :apexpages.currentpage().getparameters().get('id')];
        //opportunity op=[select id,Shipment_Info__c from opportunity where id=:apexpages.currentpage().getparameters().get('id')];
        //---------------------Added by sumit----------------------
        stringList = new List<String>();
        if (op.size() > 0)
            if (op[0].Shipment_Info__c != NULL && op[0].Shipment_Info__c != '') {
                List<String> stringList = (op[0].Shipment_Info__c).split(',');
                for (integer i = 0; i < stringList.size(); i++) {
                    all = all + stringList[i] + '<br/>';
                }
                all = all.remove('null');
            }      //------------------------------------------------------------
    }
    public Id OppId {get; set;}
    public InvoiceController(ApexPages.StandardController sc) {
        oppId = Apexpages.currentPage().getparameters().get('Id');
    }
    public List<wrapperClass> getappointmentList () {
        wrapperClass tempObj ;
        tempObj = new wrapperClass();
        List<wrapperClass> aReList = new List<wrapperClass>();
        List<Opportunity> opp = [select id, Name, AccountId from Opportunity where id = :oppid limit 1];
        List<opportunity> Opp1 = [select id, name, CreatedDate from opportunity where id = :oppid];
        //Opportunity opp1=[select id,Name,AccountId from Opportunity where id=:oppid limit 1];
        //Opportunity Opp=[select id,name,CreatedDate from opportunity where id=:oppid ];
        List<OpportunityLineItem> OPplineitem = [SELECT OpportunityId, Quantity, PricebookEntry.Product2.Name, Sys_total_Amount__c, LN__c, Extension__c, ListPrice
                                                , Pick__c, Schedule_Ship_date__c
                                                FROM OpportunityLineItem where OpportunityId = :oppId];
        system.debug('HHHHHHHHHH' + OPplineitem.size());
        for (integer i = 0; i < OPplineitem.size(); i++) {
            tempObj = new wrapperClass();
            tempObj.productname = OPplineitem[i].PricebookEntry.Product2.Name;
            tempObj.quantity = OPplineitem[i].Quantity;
            tempObj.totalamount = OPplineitem[i].Sys_total_Amount__c;
            tempObj.unitprice = OPplineitem[i].ListPrice;
            tempobj.extension = OPplineitem[i].Extension__c;
            tempobj.LineNumber = OPplineitem[i].LN__c;
            tempobj.Pick = OPplineitem[i].Pick__c;
            tempobj.ScheduledShipDate = Opplineitem[i].schedule_ship_date__c;

            if (tempobj != null) {
                aReList.add(tempObj);
            }
        }
        return aReList;
    }

    public List<wrapperClass> getappointmentList1 () {
        wrapperClass tempObj1 ;
        List<wrapperClass> aReList1 = new List<wrapperClass>();
        List<Opportunity> opp = [select id, Name, AccountId, Shipment_Info__c,
                                 Shipping_Address_Line1__c,
                                 Shipping_Address_Line2__c,
                                 Shipping_Address_Line3__c,
                                 Shipping_City__c,
                                 Shipping_State_Province__c,
                                 Shipping_Zipcode__c,
                                 Shipping_Country__c
                                 from Opportunity where id = :oppid];
        for (integer i = 0; i < opp.size(); i++) {
            tempObj1 = new wrapperClass();
            tempObj1.billingAddress = opp[i].Shipment_Info__c;

        }
        aReList1.add(tempObj1);
        /* Below code commented by Kiranmai as Shipment Info field is split into multiple fields
             //------- Added by Sumit-----------
        stringList= new List<String>();

        if(aReList1[0].billingAddress!=NULL && aReList1[0].billingAddress!=''){
        List<String> stringList=(aReList1[0].billingAddress).split(',');
        for(integer i=0;i<stringList.size();i++){
        all=all+stringList[i]+'<br/>';
        }
        } */
        all = all + opp[0].Shipping_Address_Line1__c + '<br/>';
        if (opp[0].Shipping_Address_Line2__c != NULL && opp[0].Shipping_Address_Line2__c != '') {
            all = all + opp[0].Shipping_Address_Line2__c + '<br/>';
        }
        if (opp[0].Shipping_Address_Line3__c != NULL && opp[0].Shipping_Address_Line3__c != '') {
            all = all + opp[0].Shipping_Address_Line3__c + '<br/>';
        }
        if (opp[0].Shipping_City__c != NULL && opp[0].Shipping_City__c != '') {
            all = all + opp[0].Shipping_City__c + '<br/>';
        }
        if (opp[0].Shipping_State_Province__c != NULL && opp[0].Shipping_State_Province__c != '') {
            all = all + opp[0].Shipping_State_Province__c + '<br/>';
        }

        if (opp[0].Shipping_Zipcode__c != NULL && opp[0].Shipping_Zipcode__c != '') {
            all = all + opp[0].Shipping_Zipcode__c + '<br/>';
        }
        if (opp[0].Shipping_Country__c != NULL && opp[0].Shipping_Country__c != '') {
            all = all + opp[0].Shipping_Country__c + '<br/>';
        }
        /* all = all + opp[0].Shipping_City__c + ', '+opp[0].Shipping_State_Province__c + ' - ' + opp[0].Shipping_Zipcode__c + '<br/>';
        all = all + opp[0].Shipping_Country__c + '<br/>';*/
        all = all.remove('null');


        return aReList1;
    }

    public class wrapperClass {
        public String productname {get; set;}
        public Decimal Quantity {get; set;}
        public Decimal totalamount {get; set;}

        public Decimal unitprice {get; set;}
        public date todaysdate {get; set;}
        public decimal extension {get; set;}
        public string LineNumber {get; set;}
        public string billingAddress {get; set;}
        public string splittedaddress {get; set;}
        public string wholeaddress {get; set;}
        public boolean Pick {get; set;}
        public date ScheduledShipDate {get; set;}
        public wrapperClass() {}
    }
}
  • 2
    Adding the full error message and stack trace would be helpful as it will contain the Class and line number that the error occurred at. – BarCotter Sep 16 '14 at 11:14
  • @BarCotter:PFA Screen shot .In the Error :it didnot mention any Error line number. – nikkey Sep 16 '14 at 11:23
  • 1
    Did you try turning on debug logs to see if the full error was printed there? – BarCotter Sep 16 '14 at 11:54
3

You need to look at the debug log where the error occur. At the moment there are too many potential places in your code where it could be.

But in general this error occurs if you access an empty list directly like this: opp[0].SomeField__c

Try to check whether the lists List<opportunity> op and List<Opportunity> opp are returning some records.

  • 3
    My guess is it is either the aReList1[0] or opp[0] reference. We can't know for sure until the debug logs are checked. You should always perform a null check and verify the list isn't empty before referencing it like that to avoid this type of error. – Jesse Altman Sep 16 '14 at 12:00
  • @JesseAltman Yup, but aReList1[0] is inactive (commented out). – Sergej Utko Sep 16 '14 at 12:04
  • Ah, I missed that! No syntax highlighting makes it easy to miss that stuff sometimes! Good catch. – Jesse Altman Sep 16 '14 at 12:05
  • @JesseAltman syntax highlighting - I wish it could be there!!! – Sergej Utko Sep 16 '14 at 12:06
1

Add Opportunity Id in the URL like below.....

/apex/InvoiceReportUS?id=OpportunityIdHere

By passing Opportunity Id Value in the URL, the SOQL will query the records...

  • I agree with Murali, you definitely have to do this. With that said, it would be a good idea to add error handling as well to handle the situation when the Id is not present. – Jesse Altman Sep 16 '14 at 12:21
  • @murali,2jesse Altman,@mastr0r:I tried giving the id in the url (i.e.c.cs10.visual.force.com/apex/InvoiceReport?id=Opportunity.Id) but i get an ERROR as :Visualforce Error :Id value is not valid for the Opportunity standard controller .Even tried passing the value it gives the same Error. – nikkey Sep 16 '14 at 15:39
  • @nikkey , Try with the following URL and Give your opportunity Id like below..... c.cs10.visual.force.com/apex/InvoiceReport?id=0069000000BDTfi - If Again u get error kindly specify the line number....... – Murali Mohan M Sep 17 '14 at 5:35
  • @Murali :I created a record ,i tried with the URL by giving the record id it worked now.Im thankful to everyone for your needful help – nikkey Sep 17 '14 at 8:17

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.