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
Victor19Victor19 

System.NullPointerException: Attempt to de-reference a null object error

Hi,

I am trying to setup a functionality when the user creates a child record from the child related list on the parent page,
some fields in the child record get prepopulated with the details from the parent record. I am having no issues doing this.
However the user should be able to change the parent (lookup field) from the child record and when the user does that I have a button that updates the child record fields with the new parent information.

I keep getting an Attempt to dereference a null object error.

Can someone please point out any corrections that I can do to my code to make it work.


public class Test_Controller {
    public Test_Controller(ApexPages.StandardController controller) {
    Child__c child = (Child__c)controller.getRecord();
        if (child.parent__c != null) {
            Parent__c parent = [select Name__c, TField1__c, FROM Parent__c  
                                                  WHERE id =: child.parent__c limit 1];

            child.parent__c = parent.Id;
            child.Name__c = parent.Name;
            child.TField1__c = parent.TField1__c;

        }
    }
 
    public PageReference UpdateParentInfo(){
        child = (Child__c)std.getRecord();
            List<Parent__c> parent = [SELECT Name__c, TField1__c, FROM Parent__c  
                                                      WHERE id =: child.parent__c];
        
            if(!parent.isEmpty()){
                child.Name__c = parent.get(0).Name;
                child.TField1__c = parent.get(0).TField1__c;
           
            }

    }
}


Best Answer chosen by Admin (Salesforce Developers) 
gbu.varungbu.varun

Hi 

try the following code.

 

 

public class Test_Controller {
    public Test_Controller(ApexPages.StandardController controller) {
    
        if ((Child__c)controller.getRecord() <> null) {

          Child__c child = (Child__c)controller.getRecord();
            Parent__c parent = [select Name__c, TField1__c, TField2__c, TField3__c, Tfield4__c, FROM Parent__c  
                                                  WHERE id =: child.parent__c limit 1];

            child.parent__c = parent.Id;
            child.Name__c = parent.Name;
            child.TField1__c = parent.TField1__c;
            child.TField2__c = parent.TField2__c;
            child.TField3__c = parent.TField3__c;
            child.TField4__c = parent.TField4__c;
        }
    }

All Answers

AshlekhAshlekh

Hi,

You have mentioned Parent Object as a standard controller
<apex:page standardcontroller="Parent__c" extensions="Test_Controller">

and In your Test_controller you will get the record of Parent object not child object record but you are casting in child object
Child__c child = (Child__c)controller.getRecord(); //here you will get the record of parent object.

And you are using Child__c in your Page how can you use this.

If I am worng then Please correct me

Thank you.

gbu.varungbu.varun

Hi 

try the following code.

 

 

public class Test_Controller {
    public Test_Controller(ApexPages.StandardController controller) {
    
        if ((Child__c)controller.getRecord() <> null) {

          Child__c child = (Child__c)controller.getRecord();
            Parent__c parent = [select Name__c, TField1__c, TField2__c, TField3__c, Tfield4__c, FROM Parent__c  
                                                  WHERE id =: child.parent__c limit 1];

            child.parent__c = parent.Id;
            child.Name__c = parent.Name;
            child.TField1__c = parent.TField1__c;
            child.TField2__c = parent.TField2__c;
            child.TField3__c = parent.TField3__c;
            child.TField4__c = parent.TField4__c;
        }
    }

This was selected as the best answer
Victor19Victor19
Thanks Varun. Worked as expected!
gbu.varungbu.varun

Thanks Victor

Victor19Victor19

Varun,

I am having an issue with this code. Whenever I update a field in the child record and save the record, it defaults the field value back to the parent value.

Can you suggest a fix for this issue. Basically the user should also be able to change the values of fields that have been already defaulted with the parent values

gbu.varungbu.varun

Hi Victor,

 

When you update the child record. It is calling the same controller and it put parent values again as:

child.Name__c = parent.Name;
child.TField1__c = parent.TField1__c;

 

You have to change the logic for updating record. You can create a new button to update child record and call another method to save the result.