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
Brooks Johnson 6Brooks Johnson 6 

Help With a Null Pointer Error

Hi Friends, 

I know null pointer errors are such a common question I hate to add another one. But I can't figure out what I am doing wrong and I am hoping I can learn something here.  I'm getting the error from my final For Loop. I thought I had checked for it with the != null in my If statement. Hoping someone can point me in the right direction.
 
public  class UniqueEmailCountHandler {
    
    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('Number of relationship owners in trigger = ' + roList.size());
        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 %'];
        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('Subect Line found ' + t.Subject);              
                
            }
            Set<String> subjects = subjectLineMap.get(OwnerId);
            subjects.add(t.Subject);
            subjectLineMap.put(OwnerId, Subjects);
            
        }
        system.debug('Map size   =' + subjectLineMap.size());
        system.debug('map values =' + subjectLineMap.values());
        system.debug('map keys   =' + subjectLineMap.Keyset());
        {
            for(relationship_owner__c relationshipOwner : roList){
                if(subjectLIneMap.get(relationshipOwner.Id).size() != Null){
                relationshipOwner.Unique_Emails_Sent__c = subjectLIneMap.get(relationshipOwner.Id).size();
                }
                
            }

 
Best Answer chosen by Brooks Johnson 6
Abdul KhatriAbdul Khatri
I added some extra null check making sure the code doesn't crash. I am little confuse of "{" on line number 37 from your code what is that for. Here is the code. Please let us know how it went
 
public  class UniqueEmailCountHandler {
    
    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('Number of relationship owners in trigger = ' + roList.size());
        system.debug('Contacts found = ' + contactMap.size());
        
        if(contactMap.isEmpty()) return;
        
        //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 %'];
        system.debug('Tasks Found  and added to map = ' + taskList.size());
        
        if(taskList == null || taskList.size() == 0) return;
        
        //use set to dedupe the list
        Map<Id, Set<String>> subjectLineMap = new Map<Id, Set<String>>();
        
        for(task t : taskList)
        {
            if(contactMap.get(t.WhoId) == null || 
                     contactMap.get(t.WhoId).New_Relationship_Owner1__c == null) continue;
            
            Id ownerId = contactMap.get(t.WhoId).New_Relationship_Owner1__c;            
            if(!subjectLineMap.containsKey(ownerId)){
                subjectLineMap.put(ownerId, new Set<String>());
                system.debug('Subect Line found ' + t.Subject);              
                
            }
            Set<String> subjects = subjectLineMap.get(OwnerId);
            subjects.add(t.Subject);
            subjectLineMap.put(OwnerId, Subjects);
            
        }
        system.debug('Map size   =' + subjectLineMap.size());
        system.debug('map values =' + subjectLineMap.values());
        system.debug('map keys   =' + subjectLineMap.Keyset());
        
        if(subjectLineMap.isEmpty()) return;
        
        for(relationship_owner__c relationshipOwner : roList){
            
            if(subjectLineMap.get(relationshipOwner.Id) == null) continue;
            
            relationshipOwner.Unique_Emails_Sent__c = subjectLIneMap.get(relationshipOwner.Id).size();
           
        }
    }
}

 

All Answers

Alain CabonAlain Cabon
Hi,
 
for(relationship_owner__c relationshipOwner : roList){
 if(subjectLIneMap.containsKey(relationshipOwner.Id) && 
    subjectLIneMap.get(relationshipOwner.Id).size() != Null)  {
         relationshipOwner.Unique_Emails_Sent__c = subjectLIneMap.get(relationshipOwner.Id).size();
 }                
}

 
Abdul KhatriAbdul Khatri
I added some extra null check making sure the code doesn't crash. I am little confuse of "{" on line number 37 from your code what is that for. Here is the code. Please let us know how it went
 
public  class UniqueEmailCountHandler {
    
    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('Number of relationship owners in trigger = ' + roList.size());
        system.debug('Contacts found = ' + contactMap.size());
        
        if(contactMap.isEmpty()) return;
        
        //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 %'];
        system.debug('Tasks Found  and added to map = ' + taskList.size());
        
        if(taskList == null || taskList.size() == 0) return;
        
        //use set to dedupe the list
        Map<Id, Set<String>> subjectLineMap = new Map<Id, Set<String>>();
        
        for(task t : taskList)
        {
            if(contactMap.get(t.WhoId) == null || 
                     contactMap.get(t.WhoId).New_Relationship_Owner1__c == null) continue;
            
            Id ownerId = contactMap.get(t.WhoId).New_Relationship_Owner1__c;            
            if(!subjectLineMap.containsKey(ownerId)){
                subjectLineMap.put(ownerId, new Set<String>());
                system.debug('Subect Line found ' + t.Subject);              
                
            }
            Set<String> subjects = subjectLineMap.get(OwnerId);
            subjects.add(t.Subject);
            subjectLineMap.put(OwnerId, Subjects);
            
        }
        system.debug('Map size   =' + subjectLineMap.size());
        system.debug('map values =' + subjectLineMap.values());
        system.debug('map keys   =' + subjectLineMap.Keyset());
        
        if(subjectLineMap.isEmpty()) return;
        
        for(relationship_owner__c relationshipOwner : roList){
            
            if(subjectLineMap.get(relationshipOwner.Id) == null) continue;
            
            relationshipOwner.Unique_Emails_Sent__c = subjectLIneMap.get(relationshipOwner.Id).size();
           
        }
    }
}

 
This was selected as the best answer
Brooks Johnson 6Brooks Johnson 6
Thanks, everyone. That solved it. I am sorry it took me so long to respond.