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

Help with "System.NullPointerException: Attempt to de-reference a null object"

Hi all, I am trying to learn development while building Apex classes. I got this error upon trying to deploy a custom controller and test class from sandbox to production:
"System.NullPointerException: Attempt to de-reference a null object"

Here is my controller:
public class MyController {

public final Contact c;
ApexPages.StandardController sc;
MyController m;
public MyController(ApexPages.StandardController sc) {
    this.c = (Contact)sc.getRecord();

public void quicksave() {
    Job_Applicant__c j = (Job_Applicant__c) sc.getRecord();
    update j.Contact_Candidate__r;

Here is my test class for the controller:
public class testMyController{
public static testMethod void testMyController() {

    Contact c = new Contact(LastName = 'testmycontroller');
    insert c;
    Job_Applicant__c j = new Job_Applicant__c(Contact_Candidate__c =;
    insert j;
        ApexPages.StandardController sc = new ApexPages.standardController(c);
    MyController m = new MyController(sc);
    System.assertEquals(m.c, c);

Best Answer chosen by RichardR1
this has been solved by creating a proper test class. Thanks

All Answers

AnudeepAnudeep (Salesforce Developers) 
Can you point out the line that is erroring out based on the exception stack trace?

The error “System.NullPointerException: Attempt to de-reference a null object” normally occurs when you try to reference an object which has not been initialized or has null values.

To avoid this you need to make sure that all the sObjects in your class are initialized, preferably in the constructor.

See this help article to learn more

Let me know if this helps, if it does, please close the query by marking it as solved. It may help others in the community. Thank You!
this has been solved by creating a proper test class. Thanks
This was selected as the best answer