I am hoping that someone can teach me a little about how to work through this null pointer error. The class is called from a trigger handler on After Update because I need access to all ID fields. The error is getting thrown at the final for loop where I am trying to assign the values of the map to the field unique_emails_sent__c. Add the appropriate records to a list and then update the list. The default value of the field is set to 0. Any help or teaching moments would be appreciated.

public static void uniqueRelationshipOwnerEmails(List<relationship_owner__c> roList){
        //count the unique emails  that have been sent by a relationship owner
        // add all contacts associated with relationship owner to map.         
        Map<id, Contact> contactMap = new Map<Id, Contact>([SELECT Id,
                                                            New_Relationship_Owner1__c
                                                            FROM Contact
                                                            WHERE New_Relationship_owner1__c IN : roList]);
        system.debug('Contacts found = ' + contactMap.size());
        //put tasks where whoId is in the contact map into a new map
        List<Task> taskList = [SELECT Id, WhoId,Subject
                               FROM Task 
                               WHERE WhoId IN :contactMap.keySet()
                              AND Subject LIKE '%Pardot List Email%'];
        system.debug('Tasks Found  and added to map = ' + taskList.size());
        //use set to dedupe the list
        Map<Id, Set<String>> subjectLineMap = new Map<Id, Set<String>>();

        for(task t : taskList){
            Id ownerId = contactMap.get(t.WhoId).New_Relationship_Owner1__c;            
            if(!subjectLineMap.containsKey(ownerId)){
                subjectLineMap.put(ownerId, new Set<String>());


            }

        }
        system.debug('Map size   =' + subjectLineMap.size());
        system.debug('map values =' + subjectLineMap.values());
        system.debug('map keys   =' + subjectLineMap.Keyset());
        List<relationship_owner__c> updatedRelationshipOwners = new List<relationship_owner__c>();
        for(relationship_owner__c r : roList){
            r.Unique_Emails_Sent__c = subjectLineMap.get(r.Id).size();
           updatedRelationshipOwners.add(r);

        }
        if(updatedRelationshipOwners != null){
          update(updatedRelationshipOwners);
       }





    }
}

I edited the for loop to try and check for the null but I am still getting the error.

for(relationship_owner__c r :  roList){    
            if(!subjectLineMap.containsKey(r.id)){
            r.Unique_Emails_Sent__c = subjectLineMap.get(r.Id).size();
            updatedRelationshipOwners.add(r);
            }
  • 1
    Check containskey like your checking at if(!subjectLineMap.containsKey(ownerId)){ subjectLineMap.put(ownerId, new Set<String>()); } – codeyinthecloud Oct 18 at 18:36
  • 1
    It doesn't answer this question, but I previously wrote a sort of narrative of tracing data through Apex to find the elusive null value in troubleshooting this error. – David Reed Oct 18 at 18:37
  • Thank you everyone. That link is very useful. I tried to check my map for a null but I am still getting that error. I will post the edited code block above – Brooks Johnson Oct 18 at 18:49
  • 1
    Well your'e checking for nagation(!) which translates to If it doesn't contain key. you should remove the negation. Because we want to get the value only if the key exists if(subjectLineMap.containsKey(r.id)){ r.Unique_Emails_Sent__c = subjectLineMap.get(r.Id).size(); updatedRelationshipOwners.add(r); } – codeyinthecloud Oct 18 at 18:54
  • Thank you for the help everyone – Brooks Johnson Oct 18 at 19:05
up vote 4 down vote accepted

The reason your'e getting a Null pointer exception was you are trying to get a value before checking if ever existed or not. To check that you need to check if the value existed in the map keyset.

Like i mentioned in the comments your'e checking for nagation(!) which translates to If it doesn't contain key. you should remove the negation. Because we want to get the value only if the key exists

if(subjectLineMap.containsKey(r.id)){
r.Unique_Emails_Sent__c = subjectLineMap.get(r.Id).size(); 
updatedRelationshipOwners.add(r); 
}

You're actually reversing your validity check

When you do:

if(!subjectLineMap.containsKey(r.id)){

You are, in effect, saying "Do this if I do not find the key".

Change that if statement to

if(subjectLineMap.containsKey(r.id)){

And it will work. Effectively, that is saying "do this if I do find the key".

If this line is the one throwing the NPE:

r.Unique_Emails_Sent__c = subjectLineMap.get(r.Id).size();

The most likely culprit is that subjectLineMap doesn't contain an entry for that id and then you're trying to call .size() on null.

for (relationship_owner__c r :  roList) {    
    if (subjectLineMap.containsKey(r.id)) { // only execute the following code if it _does_ contain the id
        r.Unique_Emails_Sent__c = subjectLineMap.get(r.Id).size();
        updatedRelationshipOwners.add(r);
    }
}

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.