You need to sign in to do that
Don't have an account?
SV 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;
}
}
}
"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;
}
}
}
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
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
"System.NullPointerException: Attempt to de-reference a null object"
Is there any other way to resolve the error.
Try this one.
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