I'm having trouble testing this code. It seems so simple but I can't get it to work.It keep giving me an Attempt to de-reference a null object error.

public CustomSetting__c instanceVariable;
public CustomSetting__c getCustomSettingInstance(){
     return instanceVariable;

EDIT: Forgot to add the test methods but here they are

@isTest static void test_getConfigInstance() {
    CustomSettingCtrlr myCtrlr = new CustomSettingCtrlr ();

    myCtrlr.instanceVariable.Name = 'name';
    myCtrlr.instanceVariable.booleanField= true;
    insert myCtrlr.instanceVariable;


    System.assertNotEquals(csmUtil.getConfigInstance(), null);
    System.assertEquals(myCtrlr.instanceVariable.Name, 'name');

  • Possible duplicate: salesforce.stackexchange.com/questions/174857 – sfdcfox Aug 23 '17 at 14:03
  • @AdrianLarson Duplicate? – sfdcfox Aug 23 '17 at 14:03
  • There's a little bit more going on in that post, but this scenario is one of the aspects of that post. Probably not worth reopening just to mark as duplicate though. – Adrian Larson Aug 23 '17 at 14:05
  • 1
    Please be more specific than "I can't get it to work". – Adrian Larson Aug 23 '17 at 14:19
  • 2
    This will not compile as the instanceVariable is private and thus not accessible to the test method (unless you are using an OLD API and the test is in the same class). Please post the code as it is compiled AND the EXACT error message verbatim – Eric Aug 23 '17 at 16:13

Ok, so aside from your code not compiling:

  1. Your declaration of instanceVariable starts out as null

    public CustomSetting__c instanceVariable;
  2. then in your test when you do the following

    myCtrlr.instanceVariable.Name = 'name';

You will get a DNO error as the variable is null

To Fix, add the following before that line

myCtrlr.instanceVariable = New CustomSetting__c();

Depending on what you are actually trying to do you may want to automate it and change the get method to the following:

public CustomSetting__c getCustomSettingInstance(){
     instanceVariable = CustomSetting__c.getOrgDefaults();
     //If the Id or variable is null then instantiate a new instance
     return instanceVariable;

Additionally your asserts will cause confusion as the pattern is


System.assertEquals('name', myCtrlr.instanceVariable.Name);

The way you have it reverses that order

| improve this answer | |

I'm not sure if i got it correctly i'm also a beginner but, try adding @TestVisible annotation in front of 'private CustomSetting__c instanceVariable;' in your controller.

e.g @TestVisible private CustomSetting__c instanceVariable;

then simply call the method in test class Like: CustomSettingCtrlr myCtrlr = new CustomSettingCtrlr (); myCtrlr.getCustomSettingInstance();

Hope this will help.

| improve this answer | |
  • So the private variable is public in my code, I accidentally wrote it in the code – Kush Patel Aug 23 '17 at 16:29
  • static testMethod void MyTestMethod(){ CustomSettingCtrlr CusCtrl = new CustomSettingCtrlr(); CusCtrl.instanceVariable = new CustomSetting__c(); CusCtrl.getCustomSettingInstance(); } Try this and add field if necessary! – Ajaysingh Aug 24 '17 at 13:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.