Title: Attempt to de-reference a null object error while accessing field in the Account object
Tags: account;null-pointer
Question: I have a Quote__c object which has a field named FieldTpReferAccount__c which refers to Account.
The Account object has a custom field named FieldInAccount__c.
I need to update FieldInAccount__c in account object referred in Quote__c object.
Even though the Account object reference is not null still I am getting Attempt to de-reference a null object error.
Here is the sample:
public class TempClass {
public static void someMethod(){
Quote__c quote;
Quote__c prop = [SELECT id, FieldToReferAccount__c, FieldToReferAccount__r.FieldInAccount__c
FROM Quote__c WHERE id =: quote.id];
Quote__c propupdate = new Quote__c(id = quote.id);
System.debug( 'prop.FieldToReferAccount__c=' + prop.FieldToReferAccount__c );//This is not null
System.debug( 'prop.FieldToReferAccount__r.FieldInAccount__c='
+ prop.FieldToReferAccount__r.FieldInAccount__c );//This is null
propUpdate.FieldToReferAccount__c = prop.FieldToReferAccount__c;
System.debug( 'propUpdate.FieldToReferAccount__r=' + propUpdate.FieldToReferAccount__c);//This is not null
if( null != prop.FieldToReferAccount__r.FieldInAccount__c ){
propUpdate.FieldToReferAccount__r.FieldInAccount__c = prop.FieldToReferAccount__r.FieldInAccount__c;
}
else{
propUpdate.FieldToReferAccount__r.FieldInAccount__c = '';//This line shows Attempt to de-reference a null object error
}
Why the error is here even though propUpdate.FieldToReferAccount__c is not null?

Here is the accepted answer: Firstly, the code you mentioned in the question has compilation errors i.e.
propUpdate.FieldToReferAccount__r = prop.FieldToReferAccount__c;
this above line should have given you below compilation error
Illegal assignment from Id to sObject(Your Object Name)
FieldToReferAccount__r is a relationship field hence you assign an object to it and not Id value. You have to either create a new instance of it or do a SOQL query (If you need values from this record) and assign that object to this field.
propUpdate.FieldToReferAccount__r = new YourObjectName__c(Id=prop.FieldToReferAccount__c);
YourObjectName__c objName = [select Id,... from YourObjectName__c  where Id=:prop.FieldToReferAccount__c LIMIT 1];
propUpdate.FieldToReferAccount__r = objName;
If you are trying to update the values of related object via single DML statement i.e. if you are thinking that by updating the child record, its related records will also get updated, then that does not happen. If you wish to update both records, then you will have to update the values separately. This updated values of the related records will be available only in that instance unless it is updated explicitly.
Comment for this answer: The compilation error was due to a typing mistake. The question is updated now.
I queried the Account object through SOQL and updated it.
Thanks for helping.
Comment for this answer: Awesome ! Glad to help you !!

Here is another answer: If the error is really happening on the line you're mentioning, you can assign a field to null in order to make it empty.
Update that line so that it now reads:
propUpdate.FieldToReferAccount__r.FieldInAccount__c = null;
Also, just to remove all suspicion, try putting FieldToReferAccount__c != null in the WHERE clause to ensure that field actually has a value before your code starts to handle it

{'source': 'StackExchangeDataset', 'subset': 'salesforce.stackexchange.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:07/16/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<account><null-pointer>', 'Title': 'Attempt to de-reference a null object error while accessing field in the Account object', 'ViewCount': 80, 'id': 269535, 'user_AboutMe': '<p>Working as Salesforce developer</p>\n', 'user_AccountId': 4458573.0, 'user_DisplayName': 'वरुण', 'user_Reputation': 480.0, 'user_Views': 92.0, 'user_id': 59842.0}}