You need to sign in to do that
Don't have an account?
Apex Class - Attempt to de-reference a null object error
I have a apex extension class that is supposed to save data from a visualforce page into several different objects. The main object is contract, but the class should also update the account, opportunity, and a custom object called NSA_Agencies__c. The NSA_Agencies__c field is not a required lookup field, so when I try to save a contract that has no agency, I get an exception - Attempt to de-reference a null object
How do I code my controller extension to only save if that lookup field is populated? If not populated, the code should ignore that part of the save function.
Here is my extension:
public
withsharingclassRPM_Credit {
ApexPages.StandardController controller;
publicContract contract {get; set;}
publicOpportunity opportunity {get; set;}
publicAccount account {get; set;}
publicNSA_Agencies__c agency {get; set;}
public
RPM_Credit(ApexPages.StandardController con){
controller = con;
contract = (
Contract) controller.getRecord();
opportunity = contract.Opportunity__r;
account = contract.account;
agency = contract.account.RPM_NSA_Agency__r;
}
publicPageReference saveall() {
updateopportunity;
updateaccount;
updateagency;
controller.save();
PageReference pageRef =
new PageReference('https://na5.salesforce.com/00O70000003JDRo');
returnnull;
}
}
Thank you!!
This was the only line I saw RPM_NSA_Agency__r:
agency = contract.account.RPM_NSA_Agency__r;
and in it you are not dereferencing NSA_Agency__r you are de referencing Account
So Account on Contract has no account record if RPM_NSA_Agency__r is blank? (Since you are using the child relationship name and the record in NSA_Agency__c does not exist, therefore no child exists to get an account)
You will need to do a null check for Contract if this is correct:
If (Contract.account.size() >0)
agency = contract.account.RPM_NSA_Agency__r;