You need to sign in to do that
Don't have an account?
RichardR1
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:
Here is my test class for the controller:
Thanks
"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(); } }
Thanks
All Answers
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!