3

I am getting below error..

Error:Apex trigger UpdateOverallHealth caused an unexpected exception, contact your administrator: UpdateOverallHealth: execution of BeforeUpdate caused by: System.NullPointerException: Argument cannot be null.

I am posting only relevant part of the code here... Error is coming from.. Decimal health1 = (sc.Health_Calc__c*weightFactor)/12 ;

trigger UpdateOverallHealth on Scorecard__c (after insert,after update,after delete,before insert, before update) {

    Map<Id,Opportunity> mapOpportunitiesToUpdate = new Map<Id,Opportunity>();
    Map<Id,Release__c> mapReleaseToUpdate = new Map<Id,Release__c>();
    WeightFactor__c wt = WeightFactor__c.getInstance();
    decimal weightFactor = wt.Factor__c;

 if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isbefore){

for(Scorecard__c sc :trigger.new){

                String ov_health_workflowrule = null;
                if(sc.Scope__c!=null && sc.sch__c!=null && sc.res__c!=null && sc.Risk__c!=null){
                    if(sc.Health_Calc__c !=null && sc.Health_Calc__c!=0){
                   Decimal health1 = (sc.Health_Calc__c*weightFactor)/12 ;

                   Decimal health = health1.setScale(2);

                        if(health>= 0.80 && health<=1.0){
                            ov_health_workflowrule='Green';
                        }
                        if(health>= 0.60 && health<0.80){
                            ov_health_workflowrule='Yellow';
                        }
                        if(health > 0 && health <0.60){
                            ov_health_workflowrule='Red';
                        }
                    }
                }
                sc.ov_health_workflowrule__c = ov_health_workflowrule;    

Can anyone please tell me why I am getting this error. This error didnot come up for previous 5 records created on Scorecard object , but occurred with the 6th.

Any help would be appreciated.

Thanks!

2 Answers 2

5

Weight factor is most likely null and happened on the 6th record because the first 5 did not meet the criteria to enter the IF block.

Chang this:

decimal weightFactor = wt.Factor__c

to

decimal weightFactor = wt.Factor__c == null ? 0 : wt.Factor__c;

This ternary operation will assign the value of 0 to weightFactor if wt.Factor__c is NULL otherwise it will simply assign the value of wt.Factor__c

I am assuming you just removed the dataset name from this for the post and it actually exists in your code??

WeightFactor__c wt = WeightFactor__c.getInstance();
1
  • This was really helpful.
    – login2sf
    Commented Oct 30, 2015 at 17:40
5

+1 to Eric's answer.

Depending on which operand is null you get a different exception message:

Decimal d1 = 1;
Decimal d2 = null;

// "Argument cannot be null"
Decimal d3 = d1 * d2;

// "Attempt to de-reference a null object"
Decimal d3 = d2 * d1;

which may occasionally be useful.

Presumably the * is translated into an internal-only multiply method:

// "Argument cannot be null"
Decimal d3 = d1.multiply(d2);

// "Attempt to de-reference a null object"
Decimal d3 = d2.multiply(d1);

Applies to division too.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .