You need to sign in to do that
Don't have an account?

System.NullPointerException: Attempt to de-reference a null object
Hi,
I'm adding a field from a parent object on one of our apex classes and one the corresponding visualforce page. When I am navigating to the visualforce page I checked the error message "System.NullPointerException: Attempt to de-reference a null object".
The error seems to be coming from line 620 on the apex class -
offerSiteObj.Offer__r.End_Date__c = offerSiteList[0].Offer__r.End_Date__c;
But I can't see whats wrong with this - this is to allow the user to enter a value and then when they save it will save the value on the field on the parent object.
The field is then referenced on the vf page as:
<apex:inputField value="{!offerSiteList[0].Offer__r.End_Date__c}"/>
Does anyone know what is wrong here? Am I unable to reference a field from a parent object here?
Any help is much appreciated.
check like this,
if(offerSiteList[0].Offer__r.End_Date__c!=null)
offerSiteObj.Offer__r.End_Date__c = offerSiteList[0].Offer__r.End_Date__c;
else
offerSiteObj.Offer__r.End_Date__c = '';
Thank you
All Answers
Can you please check OfferSiteList size is > 0 and Offer__r != null ?
Regards,
BDatla
Hi Rad,
You can first check if offer is not null, it can be null then null.End_Date__C can cause the issue..please check the null condition then assign. same you can check for date also..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
Hi, thanks for the replies.
Sorry I have added that condition in so my code looks like this on the apex class:
if(offerSiteList!=null && offerSiteList.size()>0){
offerSiteObj.Offer__r.End_Date__c = offerSiteList[0].Offer__r.End_Date__c;
check like this,
if(offerSiteList[0].Offer__r.End_Date__c!=null)
offerSiteObj.Offer__r.End_Date__c = offerSiteList[0].Offer__r.End_Date__c;
else
offerSiteObj.Offer__r.End_Date__c = '';
Thank you
Actually,you are referring to record which doesn't exist,i.e..,pointing the value at offerSiteList[0],which is empty. In that case of scenario this error occurs.
Use if condition like Offer__r != null to handle this kind of errors.
Thanks,
Mahendar M.
You should also do null check for offer__r then only it will work in all cases..
Please check and let me knwo if it solves your issue..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer