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
jdogsailingjdogsailing 

Controller Setter Not Called

I have an Apex page that displays fields from a structured bean returned from my controller using custom components:

 

    <apex:form >
        <apex:variable var="bean" value="{!ResponseDetail}">
            <apex:datalist var="fragment" value="{!bean.fragments}" type="1">
                <c:ResponseWidget fragment="{!fragment}" />
                <br />
            </apex:datalist>
           <apex:commandButton value="Update" action="{!updateSurvey}" />
        </apex:variable>
    </apex:form>
 

 

The controller has an instance variable with getter and setter:

 

    public SurveyBean ResponseDetail {
        get {

             ...
             return new SurveyBean(...);
        }
        set;}
   
    public PageReference updateSurvey() {
        if (ResponseDetail.fragments != null)
           return null;
        return null;
    }
 

The page displays fine, but when I press the update button I get a null pointer exception. The setter is not being called before the update method as the documentation suggests. What am I doing wrong?

 

Jeff

Best Answer chosen by Admin (Salesforce Developers) 
jdogsailingjdogsailing

It was. I modified the getter to only set it if null and now it seems to work correctly:

 

            if (ResponseDetail == null) {

                ...
                ResponseDetail = new SurveyBean(...);
            }
            return ResponseDetail;
 

Not yet obvious to me why this is the case, but thanks!

Jeff

All Answers

SuperfellSuperfell
Does the ResponseDetail getter really return a new SurveyBean every time ?
jdogsailingjdogsailing

It was. I modified the getter to only set it if null and now it seems to work correctly:

 

            if (ResponseDetail == null) {

                ...
                ResponseDetail = new SurveyBean(...);
            }
            return ResponseDetail;
 

Not yet obvious to me why this is the case, but thanks!

Jeff

This was selected as the best answer
SuperfellSuperfell
because its was updating the SurveyBean, just not the particular instance you're looking at.
jdogsailingjdogsailing

Ah, ok. How to explain my next roadblock?

 

The SurveyBean contains a list of SurveyFragments and they contain a ResponseBean object. When my VF page elaborates the SurveyBean it invokes custom components which, ultimately, render the survey as a list of input widgets. Here's one:

 

<apex:component >
    <apex:attribute name="fragment" description="This is the fragment for the component"
        type="SurveyFragment" required="true"/> 
    <apex:outputText >{!fragment.firstResponse.question}</apex:outputText><br />
    <em>{!fragment.sInstructions}</em><br />
    <apex:inputText value="{!fragment.firstResponse.score}" title="Score"/><br /> 
    <em>{!fragment.cInstructions}</em><br />
    <apex:inputTextarea value="{!fragment.firstResponse.comment}" cols="80" title="Comment"/><br /> 
</apex:component>

 

Now, when the SurveyBean gets back to my controller, none of the values which were set into its ResponseBeans are available. They all have null values. I know the SurveyBean is getting updated correctly, because it has a single checkbox widget and its state is being transmitted to the controller. Where are the other values going?

 

Jeff

Message Edited by jdogsailing on 03-14-2009 01:11 PM
Message Edited by jdogsailing on 03-14-2009 01:12 PM