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.