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
Justin StaugaitisJustin Staugaitis 

execution of BeforeInsert caused by: System.NullPointerException:

I need help changing my APEX trigger Im getting the following error message  

MassLeadConverterControllerTest
massLeadTest
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Health_Score_Calculation_New: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.Health_Score_Calculation_New: line 12, column 1: [] 
Stack Trace: Class.MassLeadConverterControllerTest.massLeadTest: line 11, column 1


Below is the Trigger 

trigger Health_Score_Calculation_New on Account (before insert) {
  if(Trigger.isBefore)
  {
    if (Trigger.isInsert || Trigger.isUpdate)
     {
       for(Account cob : trigger.new)

         {
            cob.Score__c = (cob.Implementation__c * 0.15) + 
                           (cob.Migration_In_Progress__c * 0.20) + 
                           (cob.Days_With_No_Activity__c * 0.15) + 
                           (cob.Contact_New_Rollup__c * 0.15) + 
                           (cob.Contact_Left_Rollup__c * 0.15) + 
                           (cob.Issues__c * 0.20) ;
           }
       }
    }
}
Best Answer chosen by Justin Staugaitis
Raj VakatiRaj Vakati
You can fix this error in two ways one .. 

Check for the not null values and add it as sblow below 
 
trigger Health_Score_Calculation_New on Account (before insert) {
  if(Trigger.isBefore)
  {
    if (Trigger.isInsert || Trigger.isUpdate)
     {
       for(Account cob : trigger.new)

         {
			 if(con.Contact_New_Rollup__c!=null){
				 
			 
            cob.Score__c = (cob.Implementation__c * 0.15) + 
                           (cob.Migration_In_Progress__c * 0.20) + 
                           (cob.Days_With_No_Activity__c * 0.15) + 
                           (cob.Contact_New_Rollup__c * 0.15) + 
                           (cob.Contact_Left_Rollup__c * 0.15) + 
                           (cob.Issues__c * 0.20) ;
						   }
           }
       }
    }
}

Or if the value is null then assign to zero 
 
trigger Health_Score_Calculation_New on Account (before insert) {
  if(Trigger.isBefore)
  {
    if (Trigger.isInsert || Trigger.isUpdate)
     {
       for(Account cob : trigger.new)

         {
			 if(con.Contact_New_Rollup__c==null){
				 con.Contact_New_Rollup__c= 0 ;
			 }
			 
            cob.Score__c = (cob.Implementation__c * 0.15) + 
                           (cob.Migration_In_Progress__c * 0.20) + 
                           (cob.Days_With_No_Activity__c * 0.15) + 
                           (cob.Contact_New_Rollup__c * 0.15) + 
                           (cob.Contact_Left_Rollup__c * 0.15) + 
                           (cob.Issues__c * 0.20) ;
						   }
           }
       }
    }
}