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
sumit dsumit d 

argument can not be null

Hi All ,
 i have a trigger in which I am getting error.
Trigger_Contact: execution of AfterInsert caused by: System.NullPointerException: Argument cannot be null.:
my helper class is given below:-
public class ContactTriggerHelper {
    
    public static List<Contact> newContact = new List<Contact>();
    public static List<Contact> oldContact = new List<Contact>();
    public static Map<Id, Contact> newMapContact = new Map<Id, Contact>();
    public static Map<Id, Contact> oldMapContact = new Map<Id, Contact>();
 
    public static void RollupAmount(){
        System.debug('Contact Method');
        Set<Id> Accids = new Set<Id>();
        for(Contact con : newContact){
            System.debug('Contact loopSET');
            Accids.add(Con.Accountid);
        }
        List<Account> accListToUpdate = new List<Account>();
        Map<Id,List<Contact>> mapAccountIdToAccount = new map<Id,List<Contact>>();
        // creating map with account id as key and contact as list
        for(Contact con:newContact)
        {
            if(mapAccountIdToAccount.containsKey(con.AccountId))
            {   
                mapAccountIdToAccount.get(con.AccountId).add(con);
                
            }
            else 
            {   
                mapAccountIdToAccount.put(con.AccountId,new list<contact>{con}); 
            }
        }
        for(Account acc : [select id,Rollup_Amount__c,Name,
                           (select id,Name,Amount_x__c,Type__c,Amount_Y__c from Contacts) 
                           from Account 
                           where id IN: Accids]){
                               
                                   for(Contact con : mapAccountIdToAccount.get(acc.id)){
                                       System.debug('Contact loop');
                                       if(Con.Amount_x__c != null && con.Type__c == 'Positive'){
                                           acc.Rollup_X__c = con.Amount_x__c;
                                           System.debug('+here in x');
                                       }
                                       if(Con.Amount_Y__c != null && con.Type__c == 'Negative'){
                                           acc.Rollup_Y__c = con.Amount_Y__c;
                                           System.debug('+here in Y');
                                       }
                                       acc.Rollup_Amount__c = con.Amount_x__c + con.Amount_Y__c;
                                  
                               }
                               accListToUpdate.add(acc);
                               System.debug('List to update'+accListToUpdate);
                           }
        if(accListToUpdate.Size() > 0){
            update accListToUpdate;
        }
    }
}

how to solve this error?
Le NguyenLe Nguyen
I think it come from the line of 45 : acc.Rollup_Ammount__c = con.Ammount_x__c + con.Amount_y__c;

You can initialize value of Rollup_Amount__c first and check null of Amount_x__c and Amount_Y__c;

if(acc.Rollup_Amount__c == null) acc.Rollup_Amount__c = 0;

decimal ax = con.Amount_x__c == null ? 0: con.Amount_x__c;
decimal ay = con.Amount_y__c == null ? 0 : con.Amount_y__c;

acc.Rollup_Amount__c = ax + ay;