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
krishna3krishna3 

de-reference a null object

I have been trying to find a fix for the following error.

 

I am recieving the following ereror message when I test an apex class.


System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdatePrimaryUnitOTCContact: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.UpdatePrimaryUnitOTCContact: line 29, column 30: []  and 

 

Class.TestupdatePrimaryOTCcontact.testClass: line 14, column 9 External entry point.

 

here is the scenario,

when i am writing test class for UpdatePrimaryOTCContact trigger but i am getting an error in different Trigger(PrimaryUnitOTCContact). 

 

Test Class:


 

@isTest

 

private class TestupdatePrimaryOTCcontact
{
    static testMethod void testClass()
    {  
    
          OTC_Contact__c otc = new OTC_Contact__c();
        otc.Name = '12534585';
        otc.Contact__c = '003Q000000Fqcgx';
        otc.Last_Name__c = 'test';
        otc.Is_Primary__c=TRUE;
        otc.Is_Primary_CheckPoint_Ship_To__c=TRUE;
        otc.Is_Primary_CheckPoint_Learning_Ship_To__c=TRUE;
        Insert otc;
        
        Test.startTest();
        
        Contact ct = ContactInsert();
        ct.Primary_OTC_Contact__c=otc.id;
        ct.Primary_CheckPoint_Ship_To__c=otc.id;
        ct.Primary_CheckPoint_Learning_Ship_To__c=otc.id;        
        update ct;
        
        Test.stopTest();
      }
     public static Contact ContactInsert()
    {
        Contact ct = new Contact();
        ct.LastName = '003Q000000Fqcgx';
        ct.accountid = '001Q000000FJXJY';
        ct.Phone = '1234567890';
        ct.MobilePhone = '5105489586';
        ct.Email= 'alpha@chello.be';
        
         insert ct;
        return ct;
   }
  }

 

 

 

here is the trigger that related to this class:


trigger UpdatePrimaryOTCContact on OTC_Contact__c (after insert, after update) {
    List <String> ContactIds= new List<String>();
     For(OTC_Contact__c otc : trigger.new)
     {
       ContactIds.add(otc.Contact__c);
     }
     List<Contact>Contacts = [Select Id, Name, Primary_OTC_Contact__c, Primary_CheckPoint_Ship_To__c, Primary_Checkpoint_Learning_Ship_To__c From Contact Where id IN:ContactIds];
     Map<String,Contact>ContactMap = New Map<String,Contact>(); 
     For(Contact ct : Contacts)
     {
       ContactMap.Put(ct.id, ct);
     }
     List<Contact> ConUpdates = New List<Contact>();
     For(OTC_Contact__c otc : Trigger.new)
     {
       system.debug('Entered for loop');
        if(otc.Is_Primary__c==TRUE && otc.Is_Primary_Checkpoint_Ship_To__c==TRUE
                        && otc.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE)
        {
         Contact con = ContactMap.get(otc.Contact__c);
                
                system.debug(con.Id);
                con.Primary_OTC_Contact__c=otc.id;
                con.Primary_CheckPoint_Ship_To__c=otc.id;
                con.Primary_Checkpoint_Learning_Ship_To__c=otc.id;
                ConUpdates.add(con);
         }
       else if(otc.Is_Primary__c==TRUE && otc.Is_Primary_Checkpoint_Ship_To__c==TRUE)
        {
         Contact con = ContactMap.get(otc.Contact__c);
                
                system.debug(con.Id);
                con.Primary_OTC_Contact__c=otc.id;
                con.Primary_CheckPoint_Ship_To__c=otc.id;
                ConUpdates.add(con);
         }
       else if(otc.Is_Primary_Checkpoint_Ship_To__c==TRUE && otc.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE)                        
        {
         Contact con = ContactMap.get(otc.Contact__c);
                
                system.debug(con.Id);
                con.Primary_CheckPoint_Ship_To__c=otc.id;
                con.Primary_Checkpoint_Learning_Ship_To__c=otc.id;
                ConUpdates.add(con);
         }
       else if(otc.Is_Primary__c==TRUE && otc.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE)
         {
           Contact con = ContactMap.get(otc.Contact__c);
                 
               system.debug(con.Id);
               con.Primary_OTC_Contact__c=otc.id;
               con.Primary_Checkpoint_Learning_Ship_To__c=otc.id;
               ConUpdates.add(con);
         }
     else if(otc.Is_Primary__c==TRUE)
        {
         Contact con = ContactMap.get(otc.Contact__c);
                
                system.debug(con.Id);
                con.Primary_OTC_Contact__c=otc.id;
                ConUpdates.add(con);
         }
     else if(otc.Is_Primary_Checkpoint_Ship_To__c==TRUE)
        {
         Contact con = ContactMap.get(otc.Contact__c);
                
                system.debug(con.Id);
                con.Primary_CheckPoint_Ship_To__c=otc.id;
                ConUpdates.add(con);
         }
        else if(otc.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE)
        {
         Contact con = ContactMap.get(otc.Contact__c);
                
                system.debug(con.Id);
                con.Primary_Checkpoint_Learning_Ship_To__c=otc.id;
                ConUpdates.add(con);
         }
      }
     
   update ConUpdates; 
   }

 

here is the trigger that i am getting an error saying that De-reference null Object.

 

 

trigger UpdatePrimaryUnitOTCContact on OTC_Contact__c (after insert, after update) {

 

    List <String> UnitIds= new List<String>();

     For(OTC_Contact__c otc : trigger.new)

     {

       UnitIds.add(otc.Unit__c);

     }

     List<Unit__c>Units = [Select Id, Name, Primary_OTC_Contact__c From Unit__c Where id IN:UnitIds];

     Map<String,Unit__c>UnitMap = New Map<String,Unit__c>(); 

     For(Unit__c ut : Units)

     {

       UnitMap.Put(ut.id, ut);

     }

     List<Unit__c> UnitUpdates = New List<Unit__c>();

     For(OTC_Contact__c otc : Trigger.new)

     {

       system.debug('Entered for loop');

        if(otc.Is_Primary_Unit_Contact__c==TRUE)

        {

         Unit__c un = UnitMap.get(otc.Unit__c);

 

                system.debug(un.Id);

                un.Primary_OTC_Contact__c=otc.id;

                UnitUpdates.add(un);

        }

        else

        {

          Unit__c un = UnitMap.get(otc.Unit__c);

 

                system.debug(un.Id);

                un.Primary_OTC_Contact__c=null;

                UnitUpdates.add(un);

        }     

     }

   update UnitUpdates;    

}

 

 

 

can anyone help with sample code for that error. i have to deploy that code into QA tommorow.


thanks in advance and waiting for reply.


osamanosaman

Unit__c un 

 

This object is null. You need to modify your test class so that it covers every condition in your trigger.

krishna3krishna3

thanks for your reply,

 

i tried in that way but i could not able to solve that error.

 

i you how to do that can please provide me som sample code.,

 

thanks again.

osamanosaman

Remove the code form your trigger. Put it in separate class inside a method. Call that method in your test class with different parameters.

 

Call the method in your trigger by passing the parameters.

krishna3krishna3

i am new to this apex coding, i tried this from one past one week.

 

if you have any sample code can you please post it here.

 

thanks for response and thanks for helping out with this issue.