0

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:

@isTest
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 = c.id);
    insert j;
    
        ApexPages.StandardController sc = new ApexPages.standardController(c);
    MyController m = new MyController(sc);
    System.assertEquals(m.c, c);
    
    Test.startTest();
    m.quicksave();
    test.stopTest();
}
}

Error msg: enter image description here

Thank you

2
  • 2
    Which line is the error occurring on? and are you sure it's in one of the classes you've provided? Your quicksave() method is expecting you to have passed a Job_Applicant__c to the standard controller that you've passed into your custom controller/extension, but that shouldn't be giving you a Null Pointer Exception (should be something like "illegal assignment. Cannot assign a Contact to a variable of type Job_Applicant__c").
    – Derek F
    Commented Dec 2, 2020 at 3:43
  • @DerekF thanks. Here's the actual error msg: "System.NullPointerException: Attempt to de-reference a null object Stack Trace: Class.MyController.quicksave: line 10, column 1 Class.testMyController.testMyController: line 16, column 1"
    – user90529
    Commented Dec 2, 2020 at 5:54

2 Answers 2

1

The member variable sc was never populated so is null when it is used in quickSave(). Simply populate it in the constuctor:

public MyController(ApexPages.StandardController sc) {
    this.c = (Contact)sc.getRecord();
    this.sc = sc;
}
6
  • Thanks @chun, but I tried it and still get the same error message.
    – user90529
    Commented Dec 2, 2020 at 17:54
  • Hi @Richard, is it a different error? I see that in quicksave(), sc.getRecord() is essentially the Contact c. But it tries to cast it to a Job_Applicant__c object. That looks wrong to me. Is this an extension Controller of Contact or Job_Applicant__c?
    – Chun
    Commented Dec 3, 2020 at 3:00
  • Hi @Chun it's the same error "System.NullPointerException: Attempt to de-reference a null object". The extension is for the Job_Applicant__c. I tried replacing the Contact with the Job_Applicant__c like you pointed out, it works in Sandbox but for some reason I still get the same error message when I try to deploy in Production. The error does not happen if I omit the line "update j.Contact_Candidate__r;", but if I did that then the extension becomes useless. Is it possible that the test class is the issue?
    – user90529
    Commented Dec 3, 2020 at 6:10
  • I see. This time is a different NullPointerException then. j.Contact_Candidate__r is null. The sc.getRecord() has all Job_Applicant fields but it does not automatically query its parent Contact's fields. You need to write a query to find the Contact on j. Basically sc.getRecord() in the constructor will only get you the Job_Applicant; you can only save the Job_Applicant__c but query separately on its Contact parent.
    – Chun
    Commented Dec 3, 2020 at 6:37
  • you mean like this? this.c = [SELECT LastName FROM Contact WHERE Id = :j.AVTRRT__Contact_Candidate__c];. If not, I hope you can let me know how.
    – user90529
    Commented Dec 3, 2020 at 8:17
0

Looks like the issue was in the test class, ultimately. Change set deployment was successful when I used this test class:

@isTest
public class testMyExtension{

    testmethod static void testcondition0(){
        test.startTest();
        try{
            Contact c = new Contact(LastName = 'test');
            insert c;
    
             AVTRRT__Job_Applicant__c j = new AVTRRT__Job_Applicant__c(AVTRRT__Contact_Candidate__c = c.id);
              insert j;
    
            j.AVTRRT__Contact_Candidate__c = c.id;
    
         ApexPages.StandardController sc = new ApexPages.standardController(j);
            MyExtension m = new MyExtension(sc);
            m.saveRecord();
        } catch (Exception e)
        {
            system.assert(e!=null);
        }
        test.stopTest();
    }
    
    
}

Thanks to all that made an effort to help.

You must log in to answer this question.