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
Roger EyreRoger Eyre 

I cannot delete a contact due to "System.NullPointerException: Attempt to de-reference a null object"

I have this trigger:

trigger deleteContactReceiver on Contact (before delete) 
{
    List<Contact> con=Trigger.new;
    GDPRContact.deleteContactURN(con); 
}

Invokes:

public static void deleteContactURN(List<Contact> con)
    { 
    for(Contact c:con)
        {
            try
                {
                List<TWAM_URN__c> preURN=[select URN__c, TWAM_SFID__c, TWAM_Last_Name__c, TWAM_URN_Allocation_Time__c, TWAM_URN_Is_Assigned__c from TWAM_URN__c where TWAM_SFID__c=:c.TWAM_SFID__c];
                preURN.get(0).TWAM_SFID__c='';
                System.debug('**** URN IS="+preURN.get(0).TWAM_SFID__c+" ****: ');
                preURN.get(0).TWAM_Last_Name__c='';
                preURN.get(0).TWAM_URN_Allocation_Time__c=null;
                preURN.get(0).TWAM_URN_Is_Assigned__c=FALSE;
                update preURN;
                }    
            catch(Exception e) 
                {
                System.debug('**** An unexpected error has occurred ****: ' + e.getMessage());
                }
           }  // end for 
                
    }  

The testMethod I'm trying to execute is:

public static testMethod void deleteContact()
        {
        Test.startTest();
        Contact con1=new Contact();
        con1=[select Id, AccountId from Contact where Id='0031o00001TWS6RAAX'];
        delete con1;
        List<TWAM_URN__c> postURN2=[select TWAM_SFID__c,TWAM_URN_Is_Assigned__c from TWAM_URN__c where TWAM_SFID__c=:con1.Id LIMIT 1];
        System.assertEquals(FALSE, postURN2.get(0).TWAM_URN_Is_Assigned__c);
        System.assertEquals(null, postURN2.get(0).TWAM_SFID__c);
            
        Test.stopTest();
        }

The line throwing the error is highlighted in bold. A single row in the DB does exist, verified by looking at debug logs.  I know I should not deploy like this as I may have a list of contacts, just trying to get some code working.  

Can you help?  I have spent most of the day trying to solve this one. 

Best wishes,

Roger.
Best Answer chosen by Roger Eyre
HARSHIL U PARIKHHARSHIL U PARIKH
It's a records of Trigger.Old not New. Try changing the trigger code to following block,
 
trigger deleteContactReceiver on Contact (before delete) 
{
    List<Contact> con = New List<Contact>();
    
    If(Trigger.IsBefore && Trigger.IsDelete)
     {     
        For(Contact c : Trigger.Old)
         {
           con.add(c);
          }
     }
     GDPRContact.deleteContactURN(con);
}
Hope this helps!
 

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
It's a records of Trigger.Old not New. Try changing the trigger code to following block,
 
trigger deleteContactReceiver on Contact (before delete) 
{
    List<Contact> con = New List<Contact>();
    
    If(Trigger.IsBefore && Trigger.IsDelete)
     {     
        For(Contact c : Trigger.Old)
         {
           con.add(c);
          }
     }
     GDPRContact.deleteContactURN(con);
}
Hope this helps!
 
This was selected as the best answer
Roger EyreRoger Eyre
Hi Govind,

Thanks for the help, and worked example!  This has moved me on nicely.  I now have a System assert problem but that suggests my logic code is wrong.

Best Wishes,

Roger.