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
divya gourigari 14divya gourigari 14 

getting System.NullPointerException: Attempt to de-reference a null object Trigger.emailmatchdatacontact: line 29, column 1 in trigger

execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.emailmatchdatacontact: line  29,column 1

trigger emailmatchdatacontact on ContactRelationship__c (before insert,before update) 
{
    List<ContactRelationship__c> con= new List<ContactRelationship__c>();
    List<Contact> conlist=new List<Contact>();
    Set<String> valtocheck=new Set<String>();
    Map<String,Contact> valfound=new Map<String,Contact>();
    for(ContactRelationship__c cr:trigger.new)
    {
      if(!string.isBlank(cr.EmailId__c)) valToCheck.add(cr.EmailId__c);
    }
    system.debug(valToCheck);
    Map<String, boolean> contactRelationCheckMap = new Map<String, boolean>();
    if(valtocheck != null)
    {
    List<Contact> conrelList = new List<Contact>();
    conrelList = [select id,Email from Contact where Email IN:valtocheck];
    if(conrelList.size()>0)
    {
    for(Contact c:conrelList)
    {   
        contactRelationCheckMap.put(c.Email, true);
    }
        for(ContactRelationship__c cr:trigger.new) 
        {  
            for(Contact c:conrelList)
            {
            if(cr.EmailId__c !=null)
            {
            if(contactRelationCheckMap.get(cr.EmailId__c))
            {  
                
                contact connn=new contact();
                connn.Opt_out_status__c=true; 
                connn.opt_out_transaction__c=system.today();
                connn.id=c.Id;
                conlist.add(connn);
                system.debug(c);
                system.debug(connn);
                system.debug('data available');
                system.debug(contactRelationCheckMap);
                } 
            }
            }
        }
         system.debug('conlistconlistconlistconlist');
         system.debug(conlist);
        // valfound.put(cr.Email,cr);
    } 
     // update conlist;
      //System.debug(conlist);
      
    }
    
    try
   {
    system.debug('data available');
    system.debug(conlist);
    update conlist;
    }
    catch(system.NullPointerException e)
    {
        system.debug('data not available');
    }  
}    

 
karthikeyan perumalkarthikeyan perumal
Hello, 

I request you make an Debug log for this field "cr.EmailId__c"

add debug log before this field  and let me know the Debug result value. 

System.Debug('Null Condition Value ++++++++++'  + cr.EmailId__c);
for(Contact c:conrelList)
            {
.
.
.


Thanks
karthik
 
divya gourigari 14divya gourigari 14
when iam trying to insert the data from dataloader getting error as nullpointer exception at line 27 column 1 and how can i resolve that problem
karthikeyan perumalkarthikeyan perumal
Hello, 

4 down vote accepted
In before insert trigger, trigger.newMap is null as there is no record created in the database. So your line:
 
Map<String, boolean> contactRelationCheckMap = new Map<String, boolean>();

 so use some other methods to achive this.

Consider the similer issue thread. 

https://salesforce.stackexchange.com/questions/108042/null-pointer-exception-only-while-loading-data-from-data-loader-bulk-upload 

Thanks
karthik