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
SV MSV M 

Throwing Null Pointer Exception

Hi, I've written the below class. While executing the class I am getting the error
"System.NullPointerException: Argument cannot be null." Can someone help me to resolve the issue. Below is my class,

public class UpdateEligibleContactsController {
    public void checkContactsEligible(Set<Id> contactIds) {
        Set<Id> accountIds = new Set<Id>();
        Map<Id,List<Opportunity>> accOpportunityMap = new Map<Id,List<Opportunity>>();
        //get Accounts based on ContactId
        List<Contact> contactList = [Select AccountId,Eligible__c from Contact WHERE id in:contactIds];
        for(Contact con : contactList) {
            accountIds.add(con.AccountId);
        }
        system.debug('accountIdsaccountIds'+accountIds);
        //getting opportunites from Account
        List<Opportunity> oppList = [Select AccountId,Amount from Opportunity WHERE AccountId in:accountIds];
        //Constructing map to avoid query in loop
        for(Opportunity opp : oppList) {
            Id accountId = opp.AccountId;
            List<Opportunity> accOppList = accOpportunityMap.get(accountId);
            if(accOppList == null) {
                accOpportunityMap.put(accountId,new List<Opportunity>{opp});
            } else {
                accOpportunityMap.get(accountId).add(opp);
            }
        }
        List<Contact> updateContactList = new List<Contact>();
        for(Contact con : contactList) {
            Id accountId = con.AccountId;
            List<Opportunity> accountoppList = accOpportunityMap.get(accountId);
            Decimal totalSum = 0;
            for(Opportunity opp:accountoppList){
                Decimal amount = opp.Amount;
                totalSum += amount;
            }
            if(totalSum > 5000) {
                con.Eligible__c = true;
            } else {
                con.Eligible__c = false;
            }
            
            updateContactList.add(con);
        }
        if(updateContactList.size() > 0) {
            update updateContactList;
        }
    }
}
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Sai,
 Here you are trying to calculate totalsum based on amount field value in opportunity.This error is because in your opportunity record amount is null .Try by giving adding some if condtion to check if the amount not equal to null.

Try this code
public class UpdateEligibleContactsController {
    public void checkContactsEligible(Set<Id> contactIds) {
        Set<Id> accountIds = new Set<Id>();
        Map<Id,List<Opportunity>> accOpportunityMap = new Map<Id,List<Opportunity>>();
        //get Accounts based on ContactId
        List<Contact> contactList = [Select AccountId,Eligible__c from Contact WHERE id in:contactIds];
        for(Contact con : contactList) {
            accountIds.add(con.AccountId);
        }
        system.debug('accountIdsaccountIds'+accountIds);
        //getting opportunites from Account
        List<Opportunity> oppList = [Select AccountId,Amount from Opportunity WHERE AccountId in:accountIds];
        //Constructing map to avoid query in loop
        for(Opportunity opp : oppList) {
            Id accountId = opp.AccountId;
            List<Opportunity> accOppList = accOpportunityMap.get(accountId);
            if(accOppList == null) {
                accOpportunityMap.put(accountId,new List<Opportunity>{opp});
            } else {
                accOpportunityMap.get(accountId).add(opp);
            }
        }
        List<Contact> updateContactList = new List<Contact>();
        for(Contact con : contactList) {
            Id accountId = con.AccountId;
            List<Opportunity> accountoppList = accOpportunityMap.get(accountId);
            Decimal totalSum = 0;
            for(Opportunity opp:accountoppList){
                Decimal amount = opp.Amount;
                if(opp.amount != null){  //added this condition
                totalSum += amount;
                }
            }
            if(totalSum > 5000) {
                con.Eligible__c = true;
            } else {
                con.Eligible__c = false;
            }
            
            updateContactList.add(con);
        }
        if(updateContactList.size() > 0) {
            update updateContactList;
        }
    }
}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
SV MSV M
I tried the same way you said but still, it throws the same error while executing the class.

"System.NullPointerException: Attempt to de-reference a null object"

Is there any other way to resolve the error.
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Sorry posted wrong code.Condition has to be added in 29th line.It is always a best practice to verify if it is null or not before using it .
Try this one.
public class UpdateEligibleContactsController {
    public void checkContactsEligible(Set<Id> contactIds) {
        Set<Id> accountIds = new Set<Id>();
        Map<Id,List<Opportunity>> accOpportunityMap = new Map<Id,List<Opportunity>>();
        //get Accounts based on ContactId
        List<Contact> contactList = [Select AccountId,Eligible__c from Contact WHERE id in:contactIds];
        for(Contact con : contactList) {
            accountIds.add(con.AccountId);
        }
        system.debug('accountIdsaccountIds'+accountIds);
        //getting opportunites from Account
        List<Opportunity> oppList = [Select AccountId,Amount from Opportunity WHERE AccountId in:accountIds];
        //Constructing map to avoid query in loop
        for(Opportunity opp : oppList) {
            Id accountId = opp.AccountId;
            List<Opportunity> accOppList = accOpportunityMap.get(accountId);
            if(accOppList == null) {
                accOpportunityMap.put(accountId,new List<Opportunity>{opp});
            } else {
                accOpportunityMap.get(accountId).add(opp);
            }
        }
        List<Contact> updateContactList = new List<Contact>();
        for(Contact con : contactList) {
            Id accountId = con.AccountId;
            List<Opportunity> accountoppList = accOpportunityMap.get(accountId);
            Decimal totalSum = 0;
            for(Opportunity opp:accountoppList){
                if(opp.Amount != null){   //added here
                Decimal amount = opp.Amount;
                totalSum += amount;
                }
            }
            if(totalSum > 5000) {
                con.Eligible__c = true;
            } else {
                con.Eligible__c = false;
            }
            
            updateContactList.add(con);
        }
        if(updateContactList.size() > 0) {
            update updateContactList;
        }
    }
}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
SV MSV M
Hey Chandrika, Still I am getting the same kind of error. I tried many ways but no luck.
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
I tried this in my org.Able to execute the method without any errors after adding the if condition.In which line are you getting the error?