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
RadDude89RadDude89 

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.

Best Answer chosen by RadDude89
ra811.3921220580267847E12ra811.3921220580267847E12
HI,

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

BDatlaBDatla
Hi,

Can you please check OfferSiteList size is > 0 and Offer__r != null ?

Regards,
BDatla
sandeep sankhlasandeep sankhla

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
RadDude89RadDude89

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;

ra811.3921220580267847E12ra811.3921220580267847E12
HI,

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
This was selected as the best answer
mahendar mmahendar m
Hi,

 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.
 
sandeep sankhlasandeep sankhla
Hi Rad,

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