1

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?

3

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);

or

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.

  • 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. – वरुण Jul 16 at 7:26
  • 1
    Awesome ! Glad to help you !! – Vijay Ganji Jul 16 at 10:51
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.