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
Hermann OuréHermann Ouré 

How to Fix Trigger Error: System.NullPointerException: Attempt to de-reference a null object

Hello,

I have created a trigger on Order object to update an account's custom field (Chiffre_d_affaire__c) when an order has been made.
But when trying to add a product to the order. I have an error:

User-added image

How could I fix that error?
thank you

here is my trigger:
trigger UpdateAccountCA on Order (after update) {
	
    Map<Id, Decimal> mAccAmount = new Map<Id, Decimal>();

    //Iterate through each Order 
    for(Order o : Trigger.new) {
        Decimal d = mAccAmount.get( o.AccountId );
        d += o.TotalAmount;

        mAccAmount.put( o.AccountId, d );
    }
    List<Account> lAccs = [SELECT Id, Chiffre_d_affaire__c FROM Account WHERE Id = :mAccAmount.keySet()];

    //Iterate through each List of Accounts 
    for(Account acc : lAccs){
        acc.Chiffre_d_affaire__c = acc.Chiffre_d_affaire__c + mAccAmount.get(acc.Id);
    }

    update lAccs;  
}

 
Best Answer chosen by Hermann Ouré
Gaurav Sharma 472Gaurav Sharma 472
try this:

check for syntax error. i ahve not tested it.

basically you have to check that your accountid which is acting as key in map is present or not  then proceed accordingly.
 
trigger UpdateAccountCA on Order (after update) {
	
    Map<Id, Decimal> mAccAmount = new Map<Id, Decimal>();

    //Iterate through each Order 
    for(Order o : Trigger.new) {
     if(mAccAmount.containskey(o.accountid)  {
        Decimal d = mAccAmount.get( o.AccountId );
        d += o.TotalAmount;

        mAccAmount.put( o.AccountId, d );
}

else  {

        mAccAmount.put( o.AccountId, o.TotalAmount );


    }

}
    List<Account> lAccs = [SELECT Id, Chiffre_d_affaire__c FROM Account WHERE Id = :mAccAmount.keySet()];

    //Iterate through each List of Accounts 
    for(Account acc : lAccs){
        acc.Chiffre_d_affaire__c = acc.Chiffre_d_affaire__c + mAccAmount.get(acc.Id);
    }

    update lAccs;  
}

 

All Answers

Gaurav Sharma 472Gaurav Sharma 472
how you are populating Map mAccAmount .

looks like it is null so Decimal d is assigned to null at line 7.
Hermann OuréHermann Ouré
Hi Gaurav,
I see, the value of Decimal is Null.
I did a System.debug('Decimal value ' +d);
Log: 12:54:38:004 USER_DEBUG [8]|DEBUG|Decimal value null
But how do I populate Decimal?
Gaurav Sharma 472Gaurav Sharma 472
yu are getting decimal value from mAccAmount  Map.

you need to populate it. only after that you can get value from it.


may i know what mAccAmount   should store?
Hermann OuréHermann Ouré
It should store TotalAmount from the object Order
Gaurav Sharma 472Gaurav Sharma 472
try this:

check for syntax error. i ahve not tested it.

basically you have to check that your accountid which is acting as key in map is present or not  then proceed accordingly.
 
trigger UpdateAccountCA on Order (after update) {
	
    Map<Id, Decimal> mAccAmount = new Map<Id, Decimal>();

    //Iterate through each Order 
    for(Order o : Trigger.new) {
     if(mAccAmount.containskey(o.accountid)  {
        Decimal d = mAccAmount.get( o.AccountId );
        d += o.TotalAmount;

        mAccAmount.put( o.AccountId, d );
}

else  {

        mAccAmount.put( o.AccountId, o.TotalAmount );


    }

}
    List<Account> lAccs = [SELECT Id, Chiffre_d_affaire__c FROM Account WHERE Id = :mAccAmount.keySet()];

    //Iterate through each List of Accounts 
    for(Account acc : lAccs){
        acc.Chiffre_d_affaire__c = acc.Chiffre_d_affaire__c + mAccAmount.get(acc.Id);
    }

    update lAccs;  
}

 
This was selected as the best answer
Hermann OuréHermann Ouré
Thank you Gaurav, it works!