1

I have a wrapper class of picklist values and labels. I am creating a map with picklistValues and List as key and value types. Now, I am getting an error won this line -- objResults.get(controlPicklist).add(depPicklist);

It is saying that i am referencing a null object. When I do show just this line -- >objResults.get(controlPicklist), the value is null, wherein it should return (). Unless I am constructing the key wrong?

Here are what displays on my debug logs.

ObjResults Controllers: objResults controllers : {picklistValues:[label=CustomerService, value=CustomerService]=(), picklistValues:[label=OCM, value=OCM]=(), picklistValues:[label=Facilities Management, value=Facilities Management]=(), picklistValues:[label=Accounts, value=Accounts]=(), picklistValues:[label=Marketing PR, value=Marketing PR]=(), picklistValues:[label=Talent Management, value=Talent Management]=(), picklistValues:[label=Sales, value=Sales]=(), picklistValues:[label=Admin, value=Admin]=()}


controllerValues controllers : {0=CustomerService, 1=OCM, 2=Facilities Management, 3=Accounts, 4=Marketing PR, 5=Talent Management, 6=Sales, 7=Admin}


controlPicklist : picklistValues:[label=CustomerService, value=CustomerService]


public class picklistValues{
        @AuraEnabled public String value {get;set;}
        @AuraEnabled public String label {get;set;}
    }

Map<picklistValues,List<picklistValues>> objResults = new Map<picklistValues,List<picklistValues>>();
    for(String controller : pvw.controllerValues.keySet()){
                    controllerValues.put(pvw.controllerValues.get(controller),controller);
                    picklistValues picklist = new picklistValues();
                    picklist.value = controller;
                    picklist.label = controller;
                    objResults.put(picklist,new List<picklistValues>());
                }
                system.debug('objResults controllers : ' + objResults);
                system.debug('controllerValues controllers : ' + controllerValues);

                for(fieldValues field : pvw.values){
                    for(String index : field.validFor){
                        String controllerField = controllerValues.get(Integer.valueOf(index));
                        system.debug('controllerField : ' + controllerField);

                        picklistValues controlPicklist = new picklistValues();
                        controlPicklist.label = controllerField;
                        controlPicklist.value = controllerField;
                        system.debug('controlPicklist ' + controlPicklist);

                        picklistValues depPicklist = new picklistValues();
                        depPicklist.value = field.value;
                        depPicklist.label = field.label;
                        system.debug(objResults.keySet());
                        system.debug(objResults.containsKey(controlPicklist));
                        objResults.get(controlPicklist).add(depPicklist);                    
                    }
                }

1 Answer 1

3

I think that this error is misleading. At least I'm getting nothing from it.

My first suspicion what is wrong: You're trying to use your custom created class as a map key but you don't have equals and hashcode methods implemented for it. Normally when you're using SObjects or primitive data types, you dont need to do that.

Example implemantation here:

public class PairNumbers {
Integer x,y;

public PairNumbers(Integer a, Integer b) {
    x=a;
    y=b;
}

public Boolean equals(Object obj) {
    if (obj instanceof PairNumbers) {
        PairNumbers p = (PairNumbers)obj;
        return ((x==p.x) && (y==p.y));
    }
    return false;
}

public Integer hashCode() {
    return (31 * x) ^ y;
}
}

Full article for reference here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps_keys_userdefined.htm

I would also recommend moving this wrapper to entire new class as I'm not so sure if apex is intelligent enough to connect hashcode and equals with inner class

1
  • Thank you! What I ended up doing is just change the key to a string though. Easier that way. And now, eventually, I just passed the whole Map object as string in my lightning component and do the parsing in Javascript. Whew. But this suggestion you gave is a good find! Thanks again! Commented Jul 4, 2018 at 4:01

You must log in to answer this question.

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