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
Femila Ice CreamFemila Ice Cream 

I am getting "NullPointerException: Argument cannot be null." error for following code when i am tring to save the record

trigger total on Outlet_Billing__c (before insert, before update) {



if(Trigger.isBefore){
       decimal d;
        if (Trigger.isInsert || Trigger.isUpdate)

         {

           for(Outlet_Billing__c out : trigger.new)

         // try   {
             
out.Total__c= out.WaterCandyForm__c+
out.KulfiCandyForm__c+
out.DuetForm__c+
out.BabyChocobarFormula__c+
out.BigConeForm__c+
out.Butterscotch_1000mlForm__c+
out.Butterscotch_4Ltr_BulkForm__c+
out.Butterscotch_500mlForm__c+
out.CasattaForm__c+
out.Chocolate_1000mlForm__c+
out.Chocolate4LtrForm__c+
out.Large_Cup_ButterscotchForm__c;
}
 // catch (System.NullPointerException e) {
             
   //            }

           }

 }       
Best Answer chosen by Femila Ice Cream
Dilip_VDilip_V
Hi Femila,

That is beause one of the fields you are trying to add returning null value.

so before adding that feild value to Total__C do null check.Make 0 as default value for all the number fields.


 
trigger total on Outlet_Billing__c (before insert, before update) {



if(Trigger.isBefore){
       decimal d;
        if (Trigger.isInsert || Trigger.isUpdate)

         {

           for(Outlet_Billing__c out : trigger.new)
           {
             decimal d=0;
if(out.WaterCandyForm__c!=null)
  d=out.WaterCandyForm__c;
if(out.KulfiCandyForm__c!=null)
  d=d+out.KulfiCandyForm__c;
if(out.DuetForm__c!=null)
d=d+out.DuetForm__c;
if(out.BabyChocobarFormula__c!=null)
d=d+out.BabyChocobarFormula__c;
if(out.BigConeForm__c!=null)
d=d+out.BigConeForm__c;
if(out.Butterscotch_1000mlForm__c!=null)
d=d+out.Butterscotch_1000mlForm__c;
if(Out.Butterscotch_4Ltr_BulkForm__c!=null)
d=d+Out.Butterscotch_4Ltr_BulkForm__c;
if(out.Butterscotch_500mlForm__c!=null)
d=d+out.Butterscotch_500mlForm__c;
if(out.CasattaForm__c!=null)
d=d+out.CasattaForm__c;
if(out.Chocolate_1000mlForm__c!=null)
d=dout.Chocolate_1000mlForm__c+;
if(out.Chocolate4LtrForm__c!=null)
d=d+out.Chocolate4LtrForm__c;
if(out.Large_Cup_ButterscotchForm__c!=null)
d=d+out.Large_Cup_ButterscotchForm__c;

Out.total__C=d;

           }
 }

Thanks.

All Answers

Dilip_VDilip_V
Hi Femila,

That is beause one of the fields you are trying to add returning null value.

so before adding that feild value to Total__C do null check.Make 0 as default value for all the number fields.


 
trigger total on Outlet_Billing__c (before insert, before update) {



if(Trigger.isBefore){
       decimal d;
        if (Trigger.isInsert || Trigger.isUpdate)

         {

           for(Outlet_Billing__c out : trigger.new)
           {
             decimal d=0;
if(out.WaterCandyForm__c!=null)
  d=out.WaterCandyForm__c;
if(out.KulfiCandyForm__c!=null)
  d=d+out.KulfiCandyForm__c;
if(out.DuetForm__c!=null)
d=d+out.DuetForm__c;
if(out.BabyChocobarFormula__c!=null)
d=d+out.BabyChocobarFormula__c;
if(out.BigConeForm__c!=null)
d=d+out.BigConeForm__c;
if(out.Butterscotch_1000mlForm__c!=null)
d=d+out.Butterscotch_1000mlForm__c;
if(Out.Butterscotch_4Ltr_BulkForm__c!=null)
d=d+Out.Butterscotch_4Ltr_BulkForm__c;
if(out.Butterscotch_500mlForm__c!=null)
d=d+out.Butterscotch_500mlForm__c;
if(out.CasattaForm__c!=null)
d=d+out.CasattaForm__c;
if(out.Chocolate_1000mlForm__c!=null)
d=dout.Chocolate_1000mlForm__c+;
if(out.Chocolate4LtrForm__c!=null)
d=d+out.Chocolate4LtrForm__c;
if(out.Large_Cup_ButterscotchForm__c!=null)
d=d+out.Large_Cup_ButterscotchForm__c;

Out.total__C=d;

           }
 }

Thanks.
This was selected as the best answer
Femila Ice CreamFemila Ice Cream
Thank You DILIP. It works