Below are the objects like Account, Contcat, Object1__c, Object2__c and Object3__c. Each obejct have some different field values like shown below.

Account acc = new Account();
acc.Code__c = '0011';
insert acc;

Contcat con = new Contcat();
insert con;

Object1__c Obj1 = new Object1__c();
Obj1.Account_ID__c = acc.Id;
Obj1.Contact_ID__c = con.id;
insert Obj1;

Object2__c Obj2 = new Object2__c();
Obj2.Field1__c = 'Test1 Value';
Obj2.Field2__c = 'Test2 Value';
Obj2.Field3__c = 'Test3 Value';
insert Obj2;

Object3__c Obj3 = new Object3__c();
Obj3.Object2__c = Obj2.Id;
Obj3.Object1__c = Obj1.id;

But my issue is, I want to assign a value for the field Code__c using Object3(like shown in below).

Obj3.Object1__c.Account_ID__r.Code__c = 'AAABBB';

It does not give me any issue while saving, but while I am running the code, Its giving bellow error

Attempt to de-reference a null object

Am i missing anything here? Can some one help me here please.

  • Is it really Contcat instead of the standard Contact? – Todd Sprinkel Mar 14 '19 at 17:55
  • @ToddSprinkel, Its Standard Contact – KS Kumaar Mar 18 '19 at 14:47

When you are declaring :

Obj3.Object2__c = Obj2.Id;
Obj3.Object1__c = Obj1.Id;

you are associating populating Obj3.Object1__c field with the ID of Obj1. the object has not been inserted and while there is now an id in the Obj3.Object1__c field, the reference to Obj3.Object1__r is still NULL. that is why you are getting this error.

if you were to insert obj3, there would be an Id value in Obj3.Object__c however the reference to Obj3.Object1__r is still NULL.

to accomplish what you are asking for you would need to

  1. insert Obj3;

  2. query for Obj3 ([Obj3 = SELECT Id, Object1__r.Name FROM Object3__c WHERE Id =: Obj3.Id];

at this point you should be able to read/write to parent fields without getting an error. ex:

update Obj3.Object1__r.Name;

It is still vague the context you are trying to use this in. For simplicity and directness, I would refer to @Aditya or @Radhe answer and just update the account directly:

update acc;

You can not update the parent record field value by writing dml on child record. Try below and this will update account without going into upper hierarchy.

acc.Code__c = 'AAABBB';
update acc;

seems like there is no relationship between object2,object3 with accounts and contacts.

  public static string codevalue;

      Account acc = new Account();
     acc.Code__c = '0011';

      insert acc;


you can write:


or if code__c is gloabl value,declare it and assign it directly to obj.Code__c

  • Hi, But there is no field value Code__c in object3. – KS Kumaar Mar 14 '19 at 7:12

You can't assign the values like this.

If you just want to assign the value (for solving the error you mentioned):

Obj3.Object1__r = Obj1;
Obj3.Object1__r.Obj1.Account_ID__r = acc;
Obj3.Object1__r.Obj1.Account_ID__r.Code__c = 'AAABBB';

Or if you want to update the field of parent object, use the parent object in dml operation, not with the child object like:

acc.Code__c = 'AAABBB';
update acc;

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.