5

i'm getting the following exception from my code:

caused by: System.ListException: List index out of bounds: 0

Class.tempsection4.getconvalues: line 38, column 1

My controller for the VF page is:

public class tempsection4 {

    Id opportunityid = System.currentPageReference().getParameters().get('id'); 
    public string siteName { get; set; }
    public boolean nf{ get; set;} //rename this variable. AN
    public String selectdSection { get; set; }
    public String message {get; set; }
    public String selectedItem {get; set;}
    list<SectionWrapper> listSections { get; set; }


    public tempsection4(ApexPages.StandardController controller) {
        nf = false;
        listSections = new List<SectionWrapper>();
    }


    public class SectionWrapper {
        public boolean selected { get; set; }
        public Section__c section { get; set; }
        public SelectOption selectOption { get; set; }
        public SectionWrapper (Section__c section) {
            this.selected = false;
            this.section = section;
            this.selectOption = new SelectOption(section.id, section.id);
        }
    }

    public List<SectionWrapper> getconvalues (){   

        Opportunity  [] opp= [select  Id,Site__c from Opportunity where id=:opportunityid];

       if(opp.size() > 0){
           if (opp[0].Site__c!=null){

                Section__c [] con= [select  Id, Name,Crop__c,Crop_type__c,Greenhouse_Covering__c,Site__r.Name,Section_Area__c,Site__c from Section__c where Site__c=:opp[0].Site__c];

                siteNAme = con[0].Site__r.Name;
                list<SectionWrapper> returnList = new List<SectionWrapper>();
                for (Section__c section: con)
                    returnList.add(new SectionWrapper(section));

                this.listSections = returnList;
                return this.listSections;     
             }
             else {
                siteNAme='Note that you must add a related Site to the Opportunity before you can add Products';
                return null;
             }
         }
         else {
                siteNAme='No oppurtunity found';
                return null;
         }      
    }

    public PageReference nextStep() {

       List< Section__c > selectedSection = new List< Section__c >();

        for (SectionWrapper sw: this.listSections) {
            if (sw.selected == true)
                selectedSection.add(sw.section);
        }
        if ((selectedSection.size()==0)||(selectedSection.size()>1)) { //0 OR N SECTION CHOSEN

            message = 'Note that you must add a section before you can add Products';
            String newPageUrl ='/apex/SelectSection4?id='+opportunityid;
            PageReference p = new PageReference(newPageUrl);
            return(p);
        }
        else {//ce l'hai

            // SAVE 2IDS IN SOP
            Sop__c newSop = new Sop__c();
            newSop.COsecID__c=selectedSection[0].id;
            newSop.COoppoID__c=opportunityid;

            try {
                insert newSop;
            } catch (DmlException e) {
                // Process exception here
            }

            // REDIRECT
            String newPageUrl = '/p/opp/SelectSearch?addTo='+opportunityid;
            PageReference p = new PageReference(newPageUrl);
            p.setRedirect(true);
            return (p);
        } 
    } 
}

The problem is in the line siteNAme = con[0].Site__r.Name;after the query. Please can you help me?

i'm not able to find the problem.

Thanks in advantage for any advice.

9
siteName = con[0].Site__r.Name;

con[] has no elements so trying to access the 0 index element will cause the list index out of bounds excpetion.

To protect against this you could change your code to first check to see if the returned List is not empty:

if (!con.isEmpty()) {
    siteName = con[0].Site__r.Name;
}

You may want to add in an else condition to handle the case where there is no 0th element as well which you could do in an else clause:

if (!con.isEmpty()) {
    siteName = con[0].Site__r.Name;
} else {
    // handle when con is empty        
}
4
  • Faster than I could type! ;)
    – Doug B
    Jul 5 '13 at 10:20
  • 2
    @DougB I cheated. Copied and pasted the code line from the question. ;-) Jul 5 '13 at 10:25
  • Thanks you are very kind.I have to do like this: if (con.size() > 0) { siteNAme = con[0].Site__r.Name; am i right?
    – Enry
    Jul 5 '13 at 10:31
  • @Enry - That is correct. I updated my answer to include that. Jul 5 '13 at 10:59

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.