4

I'm getting the following error when I click on the commandbutton in my Visualforce page:

EXCEPTION_THROWN [EXTERNAL]|System.TypeException: Invalid conversion from runtime type String to System.SelectOption

From an earlier question, I've learned that the setters in the controller get called with the view state before the action method gets called.

Here's the Visualforce column with the problem. If I comment it out, I don't get the error. I don't see why there would be a string to be converted in the set, but perhaps someone else here can see what I'm missing.

<apex:pageBlockTable value="{!ScholarList}" var="sl" id="table" rows="100">
...
<apex:pageBlockSectionItem >
<apex:commandButton action="{!removeSelectedNow}" value="Remove Selected Now" rerender="tablepanel,tableblock,table,panelMsg" id="theRemoveBtn"/><p/>
</apex:pageBlockSectionItem>
...   
<apex:column headervalue="Reason Removed">
<apex:selectList size="1" value="{!sl.RemoveReason}">
<apex:selectOptions value="{!RemoveOptions}"/>
</apex:selectList>    
</apex:column>apex:column>

Here's the Apex side:

//list of scholars for the controller/VF page
public List<SiteScholar> ScholarList {get; set;}
public List<SelectOption> RemoveOptions {get;set;} 
...
//wrapper class to hold individual scholar info
public class SiteScholar { 

public Boolean forWithdrawAction{get;set;}
public SelectOption RemoveReason {get;set;}
...
// method to load the selectoptions
public List<SelectOption> RemoveOptionsLoad(){
       List<SelectOption> options = new List<SelectOption>();
       options.add(new SelectOption('None','--None--'));
       Schema.DescribeFieldResult fieldResult = Scholar_Program_Application__c.Status_Change_Reason__c.getDescribe();
       List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
       for(Schema.PicklistEntry p : ple)
           options.add(new SelectOption(p.getValue(), p.getValue())); 
       return options;
 }

The selectoptions appear in the VF page, but I see the conversion error listed above when I click the command button.

4
  • Please include your markup for the commandButton.
    – Adrian Larson
    Commented Jan 5, 2016 at 21:48
  • Also, we're missing either a getRemoveOptions function or a RemoveOptions variable name.
    – sfdcfox
    Commented Jan 5, 2016 at 21:50
  • Ok, I've got those additions in place.
    – Jagular
    Commented Jan 5, 2016 at 21:57
  • Best to not have the options in the view state at all - make the field transient or just have a get method - as they are not needed to process the command. The code you have posted doesn't set the RemoveOptions field or look like it would correspond to exception you have posted.
    – Keith C
    Commented Jan 5, 2016 at 22:32

1 Answer 1

6

You should change

public SelectOption RemoveReason {get;set;}

to

public String RemoveReason {get;set;}

since the value of <apex:selectList>resolves to a String (it is independent of the <apex:selectOptions>)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .