You need to sign in to do that
Don't have an account?
sammy11
radiobuttion assigns null value
Hi,
I am working on creating question answer app, where multiple choice questions are being displayed. I have used radio button to display options for particular questions. Whenever i am assigning ID to radio it is assigned as null.
Please see below apex code where ID is assigned to radioAns string variable at line no 19:
1. private PageReference fillWrapper (Question__c pQuestion, Set<String> pAnsIdSet) {
2. system.debug('Called in fillwrapper');
3. List<SelectOption> selOptList = new List<SelectOption> ();
4. List<AnsWrapper> listOptions = new List<AnsWrapper> ();
5. String charector;
6. for (Option__c option : [SELECT Id, Correct_Answer__c, Option__c
7. FROM Option__c
8. WHERE Question__c = : pQuestion.Id]) {
9. charector = charector == null? 'A': fetchNextChar (charector);
10. AnsWrapper ans;
11. if (pQuestion.Total_Correct_Answers__c > 1) {
12. if (pAnsIdSet.contains (option.Id))
13. ans = new AnsWrapper (option, true, charector);
14. else
15. ans = new AnsWrapper (option, false, charector);
16. // listOptions.add (ans);
17. } else {
18. if (pAnsIdSet.contains (option.Id))
19. radioAns = String.valueOf(option.Id);
20. system.debug('RadioAns in fillWrapper '+radioAns+' Option.Id in fillwrapper->'+option.Id);
21. ans = new AnsWrapper (option, true, charector);
22. ans = new AnsWrapper (option, false, charector);
23. selOptList.add (new SelectOption (option.Id, option.Option__c));
24. }
25. System.debug ('listOptions ==> '+ listOptions);
26. listOptions.add (ans);
27. }
28. // if (!listOptions.isEmpty ())
29. mainWpr = new MainWrapper (pQuestion, null, listOptions);
30. // else
31. // mainWpr = new MainWrapper (pQuestion, selOptList, null);
32. return null;
33. }
Below is the VF page code where radioAns is used:
<apex:outputPanel rendered="{!mainWpr.listOption!=null&& mainWpr.question.Total_Correct_Answers__c==1}">
<apex:repeat value="{!mainWpr.listOption}" var="opt">
<div style="width:100%">
<apex:outputPanel >
<div style="width:0px; float: left; margin-top: 10px;">
<apex:outputText value="{!opt.serialNo}"/>
</div> <div style="width:90%;">
<apex:selectRadio layout="lineDirection" value="{!radioAns}" onclick="setRadioParam ('{!opt.option.Id}');">
<apex:selectOption itemLabel="{!opt.option.Option__c}" itemValue="{!opt.option.Id}"/>
</apex:selectRadio>
</div> </apex:outputPanel>
<apex:actionfunction name="setRadioParam" reRender="theForm">
<apex:param id="radioAns" name="radioAns" value="" assignTo="{!radioAns}"/>
</apex:actionfunction>
As per the apex code i tried to debug the values of option.Id and radioAns at line no 20 and below are the logs for the same:
13:25:35:058 USER_DEBUG [74]|DEBUG|OptionID: a027F00000D8xeFQAR
13:25:35:058 USER_DEBUG [74]|DEBUG|OptionID: a027F00000D8xeRQAR
13:25:35:059 USER_DEBUG [74]|DEBUG|OptionID: a027F00000D8xeSQAR
13:25:35:094 USER_DEBUG [143]|DEBUG|Called in fillwrapper
13:25:35:099 USER_DEBUG [161]|DEBUG|RadioAns in fillWrapper null Option.Id in fillwrapper->a027F00000D8xeNQAR
13:25:35:100 USER_DEBUG [166]|DEBUG|listOptions ==> ()
13:25:35:101 USER_DEBUG [161]|DEBUG|RadioAns in fillWrapper null Option.Id in fillwrapper->a027F00000D8xfWQAR
Can someone help me to figure out why null value is being assigned to radioAns when value is present in option.Id?
I am working on creating question answer app, where multiple choice questions are being displayed. I have used radio button to display options for particular questions. Whenever i am assigning ID to radio it is assigned as null.
Please see below apex code where ID is assigned to radioAns string variable at line no 19:
1. private PageReference fillWrapper (Question__c pQuestion, Set<String> pAnsIdSet) {
2. system.debug('Called in fillwrapper');
3. List<SelectOption> selOptList = new List<SelectOption> ();
4. List<AnsWrapper> listOptions = new List<AnsWrapper> ();
5. String charector;
6. for (Option__c option : [SELECT Id, Correct_Answer__c, Option__c
7. FROM Option__c
8. WHERE Question__c = : pQuestion.Id]) {
9. charector = charector == null? 'A': fetchNextChar (charector);
10. AnsWrapper ans;
11. if (pQuestion.Total_Correct_Answers__c > 1) {
12. if (pAnsIdSet.contains (option.Id))
13. ans = new AnsWrapper (option, true, charector);
14. else
15. ans = new AnsWrapper (option, false, charector);
16. // listOptions.add (ans);
17. } else {
18. if (pAnsIdSet.contains (option.Id))
19. radioAns = String.valueOf(option.Id);
20. system.debug('RadioAns in fillWrapper '+radioAns+' Option.Id in fillwrapper->'+option.Id);
21. ans = new AnsWrapper (option, true, charector);
22. ans = new AnsWrapper (option, false, charector);
23. selOptList.add (new SelectOption (option.Id, option.Option__c));
24. }
25. System.debug ('listOptions ==> '+ listOptions);
26. listOptions.add (ans);
27. }
28. // if (!listOptions.isEmpty ())
29. mainWpr = new MainWrapper (pQuestion, null, listOptions);
30. // else
31. // mainWpr = new MainWrapper (pQuestion, selOptList, null);
32. return null;
33. }
Below is the VF page code where radioAns is used:
<apex:outputPanel rendered="{!mainWpr.listOption!=null&& mainWpr.question.Total_Correct_Answers__c==1}">
<apex:repeat value="{!mainWpr.listOption}" var="opt">
<div style="width:100%">
<apex:outputPanel >
<div style="width:0px; float: left; margin-top: 10px;">
<apex:outputText value="{!opt.serialNo}"/>
</div> <div style="width:90%;">
<apex:selectRadio layout="lineDirection" value="{!radioAns}" onclick="setRadioParam ('{!opt.option.Id}');">
<apex:selectOption itemLabel="{!opt.option.Option__c}" itemValue="{!opt.option.Id}"/>
</apex:selectRadio>
</div> </apex:outputPanel>
<apex:actionfunction name="setRadioParam" reRender="theForm">
<apex:param id="radioAns" name="radioAns" value="" assignTo="{!radioAns}"/>
</apex:actionfunction>
As per the apex code i tried to debug the values of option.Id and radioAns at line no 20 and below are the logs for the same:
13:25:35:058 USER_DEBUG [74]|DEBUG|OptionID: a027F00000D8xeFQAR
13:25:35:058 USER_DEBUG [74]|DEBUG|OptionID: a027F00000D8xeRQAR
13:25:35:059 USER_DEBUG [74]|DEBUG|OptionID: a027F00000D8xeSQAR
13:25:35:094 USER_DEBUG [143]|DEBUG|Called in fillwrapper
13:25:35:099 USER_DEBUG [161]|DEBUG|RadioAns in fillWrapper null Option.Id in fillwrapper->a027F00000D8xeNQAR
13:25:35:100 USER_DEBUG [166]|DEBUG|listOptions ==> ()
13:25:35:101 USER_DEBUG [161]|DEBUG|RadioAns in fillWrapper null Option.Id in fillwrapper->a027F00000D8xfWQAR
Can someone help me to figure out why null value is being assigned to radioAns when value is present in option.Id?
@Om Prakash Its having an value, therefore it is entering into the loop, but value is set to null.
Can you review line number 18 and 19 in above code.
Line number 20 is outside of that "if" condition.