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
Ram Manohar GRam Manohar G 

Visualforce custom picklist , System.TypeException: Invalid conversion from runtime type String to List<System.SelectOption>

Hi Can anyone help me to resolve this. I am getting this error in VF controller. I am creating a custom picklist in VF controller wrapper class and  getting this
Error in debug while second run : System.TypeException: Invalid conversion from runtime type String to List<System.SelectOption>

sample code :
public class controller{
public class Wrapper
    {
      public boolean isCheck{get;set}
      public List<SelectOption> Picklist{get;set;}
      public String name{get;set;}
        }    

public List<wrapper> getwrapperlist()
    {
   
    return wrapperlist;
    } 

   public void setwrapperlist()
   {
    this.wrapperlist=wrapperlist;
   }

  public void method1()
{
 Wrapper w=new Wrapper();
w.addPicker=new List<selectoption>();
for(list<object> s:olist){
w.addPicker.add(new SelectOption (s.fname,s.fname));
}
wrapperlist.add(w);

}
}

 

Best Answer chosen by Ram Manohar G
pbattissonpbattisson
Your error is line 36. You need to have value to be some string you are writing to in the controller, not the list of selectoptions. An exmaple solution would be
 
public class controller{
public String selectedValue {get; set;}
public class Wrapper
    {
      public boolean isCheck{get;set}
      public List<SelectOption> Picklist{get;set;}
      public String name{get;set;}
        }    

public List<wrapper> getwrapperlist()
    {
   
    return wrapperlist;
    } 

   public void setwrapperlist()
   {
    this.wrapperlist=wrapperlist;
   }

  public void method1()
{
 Wrapper w=new Wrapper();
w.addPicker=new List<selectoption>();
for(list<object> s:olist){
w.picklist.add(new SelectOption (s.fname,s.fname));
}
wrapperlist.add(w);

}
}

=-----Vf page----
<apex:pageblockTable value="{!wrapperlist}" var="sw" id="pgtable" >

<apex:column headerValue="Select Name" >
             <apex:selectList multiselect="false" size="1" value="{!sw.selectedValue}">     
                    <apex:selectoptions value="{!sw.picklist}" />  
                </apex:selectlist>  
           </apex:column>

I would recommend having a read up on the <apex:selectList> component help examples (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectList.htm)

All Answers

pbattissonpbattisson
Hi Ram

I have formatted your code below. Could you check as some of it seems incorrect:
  • what is the addPicker variable on the Wrapper class?
  • olist is undefined
public class controller{
    public class Wrapper
    {
        public boolean isCheck{get;set}
        public List<SelectOption> Picklist{get;set;}
        public String name{get;set;}
    }    

    public List<wrapper> getwrapperlist()
    {
        return wrapperlist;
    } 

    public void setwrapperlist()
    {
        this.wrapperlist=wrapperlist;
    }

    public void method1()
    {
        Wrapper w = new Wrapper();
        w.addPicker=new List<selectoption>();
        for(list<object> s:olist){
            w.addPicker.add(new SelectOption (s.fname,s.fname));
        }
        wrapperlist.add(w);
    }
}

 
Ram Manohar GRam Manohar G

Hi pbattisson
Olist is having some string values (fname) which I have to add to the picklist to disply. here is the formatted code.

i am sending 'wrapperlist' to display in vf page .

 

public class controller{
public class Wrapper
    {
      public boolean isCheck{get;set}
      public List<SelectOption> Picklist{get;set;}
      public String name{get;set;}
        }    

public List<wrapper> getwrapperlist()
    {
   
    return wrapperlist;
    } 

   public void setwrapperlist()
   {
    this.wrapperlist=wrapperlist;
   }

  public void method1()
{
 Wrapper w=new Wrapper();
w.addPicker=new List<selectoption>();
for(list<object> s:olist){
w.picklist.add(new SelectOption (s.fname,s.fname));
}
wrapperlist.add(w);

}
}

=-----Vf page----
<apex:pageblockTable value="{!wrapperlist}" var="sw" id="pgtable" >

<apex:column headerValue="Select Name" >
             <apex:selectList multiselect="false" size="1" value="{!sw.Picklist}">     
                    <apex:selectoptions value="{!sw.picklist}" />  
                </apex:selectlist>  
           </apex:column>
 


 

pbattissonpbattisson
Your error is line 36. You need to have value to be some string you are writing to in the controller, not the list of selectoptions. An exmaple solution would be
 
public class controller{
public String selectedValue {get; set;}
public class Wrapper
    {
      public boolean isCheck{get;set}
      public List<SelectOption> Picklist{get;set;}
      public String name{get;set;}
        }    

public List<wrapper> getwrapperlist()
    {
   
    return wrapperlist;
    } 

   public void setwrapperlist()
   {
    this.wrapperlist=wrapperlist;
   }

  public void method1()
{
 Wrapper w=new Wrapper();
w.addPicker=new List<selectoption>();
for(list<object> s:olist){
w.picklist.add(new SelectOption (s.fname,s.fname));
}
wrapperlist.add(w);

}
}

=-----Vf page----
<apex:pageblockTable value="{!wrapperlist}" var="sw" id="pgtable" >

<apex:column headerValue="Select Name" >
             <apex:selectList multiselect="false" size="1" value="{!sw.selectedValue}">     
                    <apex:selectoptions value="{!sw.picklist}" />  
                </apex:selectlist>  
           </apex:column>

I would recommend having a read up on the <apex:selectList> component help examples (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectList.htm)
This was selected as the best answer
Ram Manohar GRam Manohar G

It's working properly in first run. In second run (when I click Next) it should get the another  fname data from olist and create another  picklist to rerender on page. .
Exception in debug :

8:16:40.072 (72258132)|CODE_UNIT_STARTED|[EXTERNAL]|Wrapper set(isCheck,false)
18:16:40.072 (72306404)|CODE_UNIT_FINISHED|Wrapper set(isCheck,false)
18:16:40.072 (72357093)|CODE_UNIT_STARTED|[EXTERNAL]|Wrapper set(picklist,Yuvan)
18:16:40.072 (72503334)|EXCEPTION_THROWN|[EXTERNAL]|System.TypeException: Invalid conversion from runtime type String to List<System.SelectOption>
18:16:40.072 (72523243)|CODE_UNIT_FINISHED|Wrapper set(picklist,Yuvan)

18:16:40.073 (73907457)|CODE_UNIT_STARTED|[EXTERNAL]|Wrapper set(isCheck,false)
18:16:40.073 (73941915)|CODE_UNIT_FINISHED|Wrapper set(isCheck,false)
18:16:40.074 (74007474)|CODE_UNIT_STARTED|[EXTERNAL]|Wrapper set(Picklist, King,Miller)
18:16:40.074 (74027111)|EXCEPTION_THROWN|[EXTERNAL]|System.TypeException: Invalid conversion from runtime type String to List<System.SelectOption>
 

 

Ram Manohar GRam Manohar G

Hi pbattisson,

Thank you very much for quick response. Now Working perfectly

vamsi krishna 106vamsi krishna 106
Hey Ram Manohar,
hey I am also getting same error..can u please explain how to slove this..please 
Thanks
Vamsi Krishna