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();
}
}
Thank you
quicksave()
method is expecting you to have passed aJob_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 aContact
to a variable of typeJob_Applicant__c
").