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
Devang Rana 6Devang Rana 6 

Trying to put Costume object data into select list but getting error in Controller

Hello every one 
      i m trying to put all my custome  object data into Select list but not abel to do.
  User-added image

My controller is this
public class DisplayBooks {  

    public String openPresentationOptions { get; set; }

    public String selectedVal { get; set; }       

    public List<SelectOption> getPresentationOptions(){    

         List<selectOption> options = new List<selectOption>();           

        for(DevTech__Book__c book : [SELECT DevTech__bookname__c,DevTech__Price__c FROM DevTech__Book__c]){

            options.add(new SelectOption(book.DevTech__bookname__c,book.DevTech__Price__c));
        }
        return options;
    }    
}


my vf Page

<apex:page controller="DisplayBooks">  
<apex:form>  
    < apex:selectList>   
    < apex:selectOptions value="{!openPresentationOptions}" />  
</apex:selectList>  
</apex:form>  
</apex:page>  
Ekta Gupta 11Ekta Gupta 11
Hi,
 The problem is you are storing data of type decimal whereas SelectOption accepts only string values
More inforamtion provided here: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_pages_selectoption.htm

Let me know if this helps you.
Nayana KNayana K
Try :
options.add(new SelectOption(book.DevTech__bookname__c,String.valueOf(book.DevTech__Price__c)));

SelectOption() takes1st 2paramaters of Type string.
Devang Rana 6Devang Rana 6
hey Ekta thanks for your help
 
Devang Rana 6Devang Rana 6
Hey Nayana from your help my controller is working. but now i m not geting  any record in selectList...
Nayana KNayana K
<apex:page controller="DisplayBooks">  
<apex:form>  
    < apex:selectList value="{!selectedVal}">   
    < apex:selectOptions value="{!PresentationOptions}" />  
</apex:selectList>  
</apex:form>  
</apex:page>
Devang Rana 6Devang Rana 6

Visualforce Error
System.NullPointerException: Argument 1 cannot be null 
Class.DevTech.DisplayBooks.getPresentationOptions: line 13, column 1

Still not getting.
 

Nayana KNayana K
public class DisplayBooks {  

    public String openPresentationOptions { get; set; }

    public String selectedVal { get; set; }       

    public List<SelectOption> getPresentationOptions(){    

         List<selectOption> options = new List<selectOption>();           
options.add('None','None');
        for(DevTech__Book__c book : [SELECT DevTech__bookname__c,DevTech__Price__c FROM DevTech__Book__c]){

            options.add(new SelectOption(book.DevTech__bookname__c,book.DevTech__Price__c));
        }
        return options;
    }    
}

<apex:page controller="DisplayBooks">  
<apex:form>  
    < apex:selectList value="{!selectedVal}">   
    < apex:selectOptions value="{!PresentationOptions}" />  
</apex:selectList>  
</apex:form>  
</apex:page>

And are you sure SELECT DevTech__bookname__c,DevTech__Price__c FROM DevTech__Book__c returning atleast one record?