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
Anand Avinash DosapatiAnand Avinash Dosapati 

Attempt to de-reference a null object: Trigger.Email2CaseSharing

Hello Gurus,

I faced the following exception when I am closing a case. The behaviour with the rest of the cases is normal. Something is missing with this, which I am not able to identify. FYR, I providing the code and the exception. Please help me understand.

Exception/Error:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Email2CaseSharing caused an unexpected exception, contact your administrator: Email2CaseSharing: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Email2CaseSharing: line 30, column 1




Code:
trigger Email2CaseSharing on Case (before insert, before update, after insert, after update) {
  
  Set<Id> contactIds = new Set<Id>();
    
    for(Case c : Trigger.new) {
        
        if(c.Origin != null && c.Origin == 'Email' && c.ContactId != null) {
            contactIds.add(c.ContactId);
        }
    }
    
    if(!contactIds.isEmpty()) {
      Map<String, String> contactEmails = new Map<String, String>();
      Map<String, User> emails = new Map<String, User>();
        for(Contact c : [SELECT Id, Email FROM Contact WHERE Id IN :contactIds]) {
            contactEmails.put(c.Id, c.Email);
            emails.put(c.Email, null);
        }
        for(User u : [SELECT Id, Email FROM User WHERE Email IN :emails.keySet()]) {
            emails.put(u.Email, u);
        }
        
        List<CaseShare> shares = new List<CaseShare>();
        
        for(Case c : Trigger.new) {
        
            if(c.Origin != null && c.Origin == 'Email' && c.ContactId != null) {
                
                Id uid = emails.get(contactEmails.get(c.ContactId)).Id;
                
                if(Trigger.isBefore) {
                  c.Submitter__c = uid;
                } else {
                  CaseShare cs = new CaseShare(
                    CaseId = c.Id,
                      UserOrGroupId = uid,
                      CaseAccessLevel = 'Read'
                  );
                  shares.add(cs);
                }
            }
        }
        
        try {
          if(!shares.isEmpty()) {
              insert shares;
          }
        } catch(Exception e) {
          System.debug(e.getStacktraceString());
        }
    }
}

 
PRAKASH JADA 13PRAKASH JADA 13
Hey,


Id uid = emails.get(contactEmails.get(c.ContactId)).Id; check this line.  this is null. becuase of this line  emails.put(c.Email, null);
Anand Avinash DosapatiAnand Avinash Dosapati
Thanks for your response, Prakash.
But on the case, the email of contact and contact Id are not null.
Mahesh NirmalMahesh Nirmal

Hi Anand,

You can track each list by using the debug log and use the null check before any operation on list.

PRAKASH JADA 13PRAKASH JADA 13
yes they are not null but you are not capturing them in the map. If you want to know it visually then add system.debug to Id uid and system.debug to  emails. I hope this helps thanks.