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
Baz DensonBaz Denson 

Uncaught Error in $A.getCallback() [Cannot read property 'Account__c' of null]

I am getting the following error:

Uncaught Error in $A.getCallback() [Cannot read property 'Account__c' of null]

when I try to set a variable in my JS Helper class of my Lightning component.

It occurs at the following lines:
 
console.log('Parent Id is '+parentRecordId);
                component.set('v.addressDetails.Account__c', parentRecordId);

If I look at the console, I can see that parentRecordId is set, and a 
field exists on the object called Account__c. 

Can anyone explain what this error indicates and how to resolve it?

Thanks

Barry 
Best Answer chosen by Baz Denson
MKRMKR
Hi,

You cannot set the attribute of addressDetails directly with component.set(). Instead, try the following:
var addressDetails = component.get('v.addressDetails');
addressDetails.Account__c = parentRecordId;
component.set('v.addressDetails', addressDetails);
Regards,
Mkr

All Answers

MKRMKR
Hi,

You cannot set the attribute of addressDetails directly with component.set(). Instead, try the following:
var addressDetails = component.get('v.addressDetails');
addressDetails.Account__c = parentRecordId;
component.set('v.addressDetails', addressDetails);
Regards,
Mkr
This was selected as the best answer
Baz DensonBaz Denson
Hi Mkr

Thanks for the response, but I get exactly the same error using your code.
MKRMKR
Hi,

Right, are addressDetails already populated? So should there be an object already assigned to it?

If not, you should do something like this:
var addressDetails = component.get('v.addressDetails');
if(addressDetails == null) {
    addressDetails = {};
}
addressDetails.Account__c = parentRecordId;
component.set('v.addressDetails', addressDetails);

Regards,
Mkr​​​​​
Baz DensonBaz Denson
Thanks Mkr

That solved it, my addressDetails object was empty.