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
SF94108SF94108 

Total failing because of null field - System.NullPointerException

Need to do a simple total of values from a query. Some database numeric fields do not have a value.

 

here's the code snippet-

trigger.new[i].total_Amount__c = Trigger.new[i].meals_cost__C +Trigger.new[i].Admin_Fee__c;

 

in this case admin_fee__C is not applicable so has null value(its a numeric field)

 

Tried most functions to get around the issue - how do I get the total working?

 

Thanks much.

 

included below is a summary of my various attempts!

 

trigger.new[i].total_Amount__c = Trigger.new[i].meals_cost__C + NULLVALUE(Trigger.new[i].Admin_Fee__c,0);

Error:
Method does not exist or incorrect signature: NULLVALUE(Decimal, Integer) at line 47 column 71


tried replacing with decimal with following local variable
decimal replaceNULL=0;
trigger.new[i].total_Amount__c = Trigger.new[i].meals_cost__C + NULLVALUE(Trigger.new[i].Admin_Fee__c,replaceNULL);
     
Error:
Method does not exist or incorrect signature: NULLVALUE(Decimal, Decimal) at line 48 column 71

same with blankvalue
trigger.new[i].total_Amount__c = Trigger.new[i].meals_cost__C + BLANKVALUE(Trigger.new[i].Admin_Fee__c,replaceNULL);

error:
Method does not exist or incorrect signature: BLANKVALUE(Decimal, Decimal) at line 48 column 71

 

if (ISNULL(Trigger.new[i].Admin_Fee__c) ) {Trigger.new[i].Admin_Fee__c=0;}

--Error:
--Method does not exist or incorrect signature: ISNULL(Decimal) at line 45 column 11

if (ISBLANK(Trigger.new[i].Admin_Fee__c) ) {Trigger.new[i].Admin_Fee__c=0;}
--similar error


also tried using len(fieldname) <1 to check if null - does not work

 

Best Answer chosen by Admin (Salesforce Developers) 
hchhch

Use the following code:

 

if(Trigger.new[i].Admin_Fee__c == null){

    Trigger.new[i].Admin_Fee__c =0;

}

trigger.new[i].total_Amount__c = Trigger.new[i].meals_cost__C +Trigger.new[i].Admin_Fee__c;

All Answers

hchhch

Use the following code:

 

if(Trigger.new[i].Admin_Fee__c == null){

    Trigger.new[i].Admin_Fee__c =0;

}

trigger.new[i].total_Amount__c = Trigger.new[i].meals_cost__C +Trigger.new[i].Admin_Fee__c;

This was selected as the best answer
SF94108SF94108

Hi hch - Did try that still the same error-

 

Apex trigger GetRegAmount caused an unexpected exception, contact your administrator: GetRegAmount: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: 

 

Thanks though

hchhch

Can you provide the complete code?

SF94108SF94108

-- here's part of code that is failing

 

trigger GetRegAmount  on Reg2__c (before insert,before update) {

Set<ID> CourseIds = new Set<ID>();

  
for(Reg2__c NewReg : Trigger.new){
       
        CourseIds.add(NewReg.Course__c);
}


MAP<id,Course__c> CourseMap = new Map<ID, Course__c>([ SELECT Id, cost__c,Housing_Cost__c, Meals_Cost__c FROM Course__c  WHERE Id IN :CourseIds]);

for (Integer i = 0; i < Trigger.new.size(); i++)  { //for all records

      //get meals cost from map
      Trigger.new[i].meals_cost__C = CourseMap.get(trigger.new[i].Course__c).Meals_Cost__c;
     
      //admin fee is a numeric data entry field - this is the line that fails
      trigger.new[i].total_Amount__c = Trigger.new[i].meals_cost__C +Trigger.new[i].Admin_Fee__c;
     
      
}
}

SF94108SF94108

yes - that did work.

 

it was a two part error, handling of code which your suggestion solved.

 

the second issue was the map field was null(which was the other value in total - so had to handle that too.

 

Thanks much.