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
LnFLnF 

Custom Setting reference creates: System.NullPointerException in Test Method

I have a page that iFrames another site and uses a simple class to get the URL and userid.  I changed the class to use the Custom Settings for the URL instead of hardcoding it into the code.  It works; but I get the following error when running the test:  Message:  System.NullPointerException: Attempt to de-reference a null object.  Stack Trace:  Class.clsPMNotes.<init>: line 14, column 1 Class.clsPMNotes.testPMNotes: line 20, column 1

 

It looks like this "PmNotesSite__c.getInstance('DefaultSiteName').Site__c" is the culprit.

 

Here's the class

 

public class clsPMNotes
{
    public string prpIFrameSourceURL {get;set;}
    public clsPMNotes()
    {
         prpIFrameSourceURL = PmNotesSite__c.getInstance('DefaultSiteName').Site__c + Userinfo.getUserId();
    }
    
    public static testMethod void testPMNotes()
    {
        Test.StartTest();
        clsPMNotes obCLS = new clsPMNotes();
        Test.StopTest();
    }
}

 

Thanks much in advance.

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

Data in Custom Settings are treated like normal objects during tests - so if you are using api v24 or above, the data is not visible to the test script

 

You either need to set your test to have access to all data, or create new values in the custom setting at the start of the test...

All Answers

BritishBoyinDCBritishBoyinDC

Data in Custom Settings are treated like normal objects during tests - so if you are using api v24 or above, the data is not visible to the test script

 

You either need to set your test to have access to all data, or create new values in the custom setting at the start of the test...

This was selected as the best answer
LnFLnF

Thanks much; I created the data and test passed with flying colors.