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
EguiEgui 

NullPointerException in test class when retrieving custom setti

Hi !

 

I'm currently running in the following issue :

 

I've defined a custom setting, hierachy type and public visibility.


It contains a text field, this field is filled with a value.


In an apex class, I retrieve this custom setting like this :

global class MyClass{
    
    private static String latitudeLongitude = CustomSetting__c.getInstance().TextField1__c;

}

 

Then I use this field in a method of this class and this is working fine, the value is correctly retrieved.


But when I lanch the following test class, a NullPointerException is thrown on the same line that retrieve the custom setting value.

This is odd because custom settings are initiazed in the test class (This is apex v27)

This is the test method :

static testMethod void TestGoogleGeoCodeUpdater(){
        //Création des custom settings pour les tests
        ALTGEOCODE__Geocode__c CS = new ALTGEOCODE__Geocode__c(
            ALTGEOCODE__Champ_coordonnees__c = 'ALTGEOCODE__Geolocation__c'
        );
        insert CS;
        
        Account compte1 = new Account(
            Name              = 'Test',
            BillingStreet     = 'Revaison Street',
            BillingPostalCode = '60000',
            BillingCity       = 'SP',
            BillingState      = 'Rhône',
            BillingCountry    = 'FRANCE'
        );
        insert compte1;
    }

 

I'm really stuck here, help would be appreciated.

 

Thanks a lot.

JohnSchultzJohnSchultz

Beginning with v24.0 of the API, custom settings are no longer accessible from tests. They are treated the same as most standard data.

 

So, you have a couple of options: you can use the IsTest(SeeAllData=True) annotation. Or the better option, in my opinion, is to create the custom setting data in your test class. This way your test is code and org independent by not having to rely on a piece of data being available for testing.

EguiEgui

Thanks for your answer but as I explained later, I did create the custom setting data in the test class.

JohnSchultzJohnSchultz

Can you show us the rest of the code? That might give us some clues as to the root of the problem.