You need to sign in to do that
Don't have an account?

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
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
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;
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
Can you provide the complete code?
-- 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;
}
}
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.