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
Michael HaaseMichael Haase 

Nullpointer Exception when using custom settings in Apex Class (namespace prefix exists)

Hello all,

I get a nullpointer exeption when writing an apex class for my trigger. The apex class and the trigger use Custom Settings.I the rror: "System.NullPointerException: Attempt to de-reference a null object". I guess the reason is that the Custom Setting object cannot be correctly allocated due to the namespace prefix (M2809) I have registered.
Does anybody have an idea how to put that correctly in the code or how to solve the problem?

Here is the apex class. I get the error in line 7 column 0.
1 @isTest
2  public class TaskTriggerTest {
3    static testMethod void testLastResearchActivity_v2(){
4     
5       //Get custom settings
6        Research_Mailer__c settings = Research_Mailer__c.getInstance('RMS');
7        String RSM_Task_Record_Type_ID = settings.m2809__Task_Record_Type_ID__c;
8        String RSM_ChuykoId = settings.Chuyko_Owner_ID__c;
9       String RSM_GoncharovId = settings.Goncharov_Owner_ID__c;
10     String RSM_ProfileId = settings.Profile_ID__c;
        
        //Create 3 users and insert them
        List<User> userToInsert = new List<User>();
        //User 1
        User u1 = new user();
        u1.FirstName = 'David';
        u1.LastName  = 'Owner 1';
     ...
AadryanAadryan
@isTest(SeeAllData=true) ??
viruSviruS
SeeAllData = true is not a good approach.

if Research_Mailer__c custom setting is heirerchy type please go and check default value or "RMS"( Profile,User) record exists  if not   then create default record or RMS 

if it is List ty[e setting then create a new instance and insert new record 
KaranrajKaranraj
Michael - You can't access your org data in the test class unless you are setting seeAllData= true in the class, which is not recommended way to write test class. Your test class will again fail if the data is not avaible in the org, so create test data for your custom settings like objects in the test class.
CustomSetting__c cs = new CustomSetting__c();
cs.Name='test';
//cs.Other fiels values
insert cs;
Thanks,
Karan
 
Michael HaaseMichael Haase
Hi Karanraj,

thats a good point. I will test that and let you know.
Thanks a lot so far.

Michael