0

I'm trying to build a custom VF quick action on the Case object that allows users to update checkboxes for case fields and also lets them add some comments that will later be posted to chatter, but not stored on the case object.

In the interest of future proofing/scalability I am using a field set to hold the check box fields. I then loop through this field set and create a wrapper class instance where I bind a String that will be used for the comments.

The page loads correctly when I declare the wrapper class as a class variable like this:

public List <taskAndComment> tacList = new List <taskAndComment>()

However when I switch it to:

public List <taskAndComment> tacList {get; set;} 

..I get a null pointer exception on page load. This occurs when I try to add it the list tacList.add(t);

When I debug the value t, the wrapper class has been created correctly.

Can anyone help?

public with sharing class GDPR_Dynamic_Extension {

    public Case thisCase {get;set;}
    public List <taskAndComment> tacList {get; set;}

    //Get the fields from FieldSet
    Schema.SObjectType SObjectTypeObj = Schema.getGlobalDescribe().get('Case');
    transient Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();
    transient Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get('GDPR_Task_Fields');

    public List<Integer> getIntegers(){
        return createIndex(fieldSetObj.getFields().size());
    }

    public GDPR_Dynamic_Extension(ApexPages.StandardController controller) {
        controller.addFields(new String[]{
                'GDPR_IS_Apps__c',
                'GDPR_TON_Head__c'
        });
        thisCase = (Case)controller.getRecord();
    }

    public List<taskAndComment> getTheList() {
        for( Schema.FieldSetMember eachFieldSetMember : fieldSetObj.getFields() ){
            String s = eachFieldSetMember.getLabel();
            Boolean b = (Boolean)thisCase.get(eachFieldSetMember.getFieldPath());
            taskAndComment t = new taskAndComment(s,b);
            tacList.add(t);
        }
        return tacList;
    }

    public class taskAndComment{
        public String fieldLabel {get; set;}
        public Boolean fieldValue {get; set;}
        public String taskComment {get; set;}

        public taskAndComment(String theFieldLabel, Boolean theFieldValue){
            fieldLabel = theFieldLabel;
            fieldValue = theFieldValue;
            taskComment = '';
        }
    }

    public static List<Integer> createIndex(Integer count){
        List<Integer> indexList = new List<Integer>();
        for (Integer i = 0; i < count; i++) {
            indexList.add(i);
        }
        return indexList;
    }
}

VF page for reference:

<apex:page id="GDPR_Dynamic" StandardController="Case" extensions="GDPR_Dynamic_Extension">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection title="Incomplete Tasks" columns="3">
                <apex:pageBlockTable value="{!Integers}" var="i" id="theRepeat">

                    <apex:column>
                        <apex:facet name="header">Name</apex:facet>
                        <apex:facet name="footer"></apex:facet>
                        <apex:outputText value="{!TheList[i].fieldLabel}"/>
                    </apex:column>
                    <apex:column>
                        <apex:facet name="header">Task</apex:facet>
                        <apex:facet name="footer"></apex:facet>
                        <apex:inputCheckbox value="{!TheList[i].fieldValue}"/>
                    </apex:column>
                    <apex:column>
                        <apex:facet name="header">Comment</apex:facet>
                        <apex:facet name="footer"></apex:facet>
                        <apex:inputText value="{!TheList[i].taskComment}"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

1 Answer 1

3

You need to set the tacList variable to reference a new list object before you add to that list. You can do that in the constructor:

thisCase = ...;
tacList = new List<taskAndComment>();
2
  • Thanks so much! That's been driving me crazy. All the best
    – Jay
    Commented Jul 15, 2018 at 9:03
  • @Jay No problem. The patterns for get/set take a while to get used to.
    – Keith C
    Commented Jul 15, 2018 at 9:19

You must log in to answer this question.

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