+ Start a Discussion
U JayU Jay 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: caused by: System.NullPointerException: Attempt to de-reference a null object

@isTest
public class TestCopyEmailToAllAssociatedTransactions{
   public static testMethod void TestCopyEmailToAllAssociatedTransactionsController(){
        contact contactRecord = new Contact();
        contactRecord.lastName = 'Test last name';
        insert contactRecord;
       
        Opportunity opportunityRecord = new Opportunity();
        opportunityRecord.Name = 'Test Opportunity';
        opportunityRecord.StageName ='Finalized';
        opportunityRecord.CloseDate = date.Today();
        opportunityRecord.Contact__c = contactRecord.id;
        insert opportunityRecord;
       
        ChargentSFA__Transaction__c transactionRecord = new ChargentSFA__Transaction__c();
        transactionRecord.ChargentSFA__Response_Message__c = 'OK';
        transactionRecord.ChargentSFA__Response_Status__c ='Approved';
        transactionRecord.ChargentSFA__Opportunity__c = opportunityRecord.id;
        insert transactionRecord;

       
        contactRecord.Email= 'test@test.com';
        update contactRecord;
   }
}
Tis is my test class. The bolded section generate a trigger failure
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ChargentSFA.TransacionTrigger: execution of AfterInsert

caused by: System.NullPointerException: Attempt to de-reference a null object
What will be the reason?
Thanks in advance.
Anoop yadavAnoop yadav
Hi,

Can you put your trigger here.
Also comment the "insert transactionRecord;" Line and then try.
U JayU Jay
Trigger : -
trigger CopyEmailToAllAssociatedTransactions on Contact (after Insert, after update) {
    if(trigger.new != null){
        CopyEmailToAllAssociatedTransactions.copyEmailToAllAssociatedTransactionsMethod(trigger.new);             
    }   
}
Trigger class :-
public without sharing class CopyEmailToAllAssociatedTransactions{
  
    public static void copyEmailToAllAssociatedTransactionsMethod(List<Contact> triggerList){
        List<ChargentSFA__Transaction__c> transactionCompleteList = new List<ChargentSFA__Transaction__c>();
        List<ChargentSFA__Transaction__c> transactionListToUpdate = new List<ChargentSFA__Transaction__c>();
        List<Opportunity> opportunityCompleteList = new List<Opportunity>();
        transactionCompleteList = [SELECT id, ChargentSFA__Opportunity__r.Contact__r.Id,ChargentSFA__Response_Status__c,
                                          ChargentSFA__Opportunity__r.Contact__r.Email,ChargentSFA__Response_Message__c,
                                          Opportunity_s_primary_contact_email__c                                         
                                   FROM ChargentSFA__Transaction__c
                                   WHERE ChargentSFA__Response_Message__c = 'OK' AND
                                         ChargentSFA__Opportunity__c != null AND
                                         ChargentSFA__Response_Status__c ='Approved'];
        for(Contact contactRecord : triggerList){
            for(ChargentSFA__Transaction__c transactionRecord : transactionCompleteList){
                 if(contactRecord.id == transactionRecord.ChargentSFA__Opportunity__r.Contact__r.Id){
                     transactionRecord.Opportunity_s_primary_contact_email__c = contactRecord.Email;
                     transactionListToUpdate.add(transactionRecord);
                 }
            }
        }
        if(transactionListToUpdate.size() > 0){
            update transactionListToUpdate;
        }
    }
}

Commented and run .Success case.But.....?
U JayU Jay
Hi Anoop & raja,

Commented and run . Success case. But.....?


Trigger : -
trigger CopyEmailToAllAssociatedTransactions on Contact (after Insert, after update) {
    if(trigger.new != null){
        CopyEmailToAllAssociatedTransactions.copyEmailToAllAssociatedTransactionsMethod(trigger.new);            
    }  
}
Trigger class :-
public without sharing class CopyEmailToAllAssociatedTransactions{
 
    public static void copyEmailToAllAssociatedTransactionsMethod(List<Contact> triggerList){
        List<ChargentSFA__Transaction__c> transactionCompleteList = new List<ChargentSFA__Transaction__c>();
        List<ChargentSFA__Transaction__c> transactionListToUpdate = new List<ChargentSFA__Transaction__c>();
        List<Opportunity> opportunityCompleteList = new List<Opportunity>();
        transactionCompleteList = [SELECT id, ChargentSFA__Opportunity__r.Contact__r.Id,ChargentSFA__Response_Status__c,
                                          ChargentSFA__Opportunity__r.Contact__r.Email,ChargentSFA__Response_Message__c,
                                          Opportunity_s_primary_contact_email__c                                        
                                   FROM ChargentSFA__Transaction__c
                                   WHERE ChargentSFA__Response_Message__c = 'OK' AND
                                         ChargentSFA__Opportunity__c != null AND
                                         ChargentSFA__Response_Status__c ='Approved'];
        for(Contact contactRecord : triggerList){
            for(ChargentSFA__Transaction__c transactionRecord : transactionCompleteList){
                 if(contactRecord.id == transactionRecord.ChargentSFA__Opportunity__r.Contact__r.Id){
                     transactionRecord.Opportunity_s_primary_contact_email__c = contactRecord.Email;
                     transactionListToUpdate.add(transactionRecord);
                 }
            }
        }
        if(transactionListToUpdate.size() > 0){
            update transactionListToUpdate;
        }
    }
}


U JayU Jay
Thank you all :) Got the solution when i changed the code so that 
       ChargentSFA__Transaction__c transactionRecord= new ChargentSFA__Transaction__c();
        transactionRecord.ChargentSFA__Amount__c=400;
        transactionRecord.Name_on_Check__c = 'tester';
        transactionRecord.ChargentSFA__Gateway_ID__c = '';
        transactionRecord.ChargentSFA__Opportunity__c=opportunityRecord.Id;
        transactionRecord.ChargentSFA__Type__c='Charge';
        transactionRecord.ChargentSFA__Response__c='Approved';
        transactionRecord.ChargentSFA__Response_Message__c = 'OK';
        transactionRecord.ChargentSFA__Response_Status__c = 'Approved';
        transactionRecord.ChargentSFA__Payment_Method__c='Check (Paper)';
        insert transactionRecord;


instead of :-
       ChargentSFA__Transaction__c transactionRecord = new ChargentSFA__Transaction__c();
        transactionRecord.ChargentSFA__Response_Message__c = 'OK';
        transactionRecord.ChargentSFA__Response_Status__c ='Approved';
        transactionRecord.ChargentSFA__Opportunity__c = opportunityRecord.id;
        insert transactionRecord;