1

I have created a Protected/List custom setting object in my org (not a managed package)

enter image description here

It's Custom fields are:

enter image description here

I created a dataset named: test with relevant values (I had another dataset with a ' ' space, thought this causes the problem.

test is a dataset in my sandbox

Details of the test dataset

When I try to access the dataset from APEX, I keep getting Null reference error.

System.NullPointerException: Attempt to de-reference a null object (error in line 15)

Code snippet: (code is in apex class - not @test)

14: Netsuite_Integration__c m_nsi  = Netsuite_Integration__c.getValues('test');
ERROR LINE: 15: system.debug('15 m_nsi.NS_Email__c:' + m_nsi.NS_Email__c);

Already tried:

  • Already Tried getValues, getInstance
  • Recreated the object again in Sandbox (known issue)
  • Created several datasets with different names (removed space)

While coding, developer console does recognizes, and autocompletes

enter image description here

ideas?

  • 2
    Is this code executing within a unit test? – Mark Keats Mar 4 '15 at 13:16
  • @MarkKeats the code is in APEX class, but it fails in every run, can't deploy, can't test, if this is what you mean – Saariko Mar 4 '15 at 13:18
  • 1
    I understand the code in inside a class, but what is executing the code? The error message implies that there is no record called 'test' for that custom setting - and the most obvious cause would be that it's executing within the context of a test. – Mark Keats Mar 4 '15 at 13:23
  • 2
    Unless you specify the seealldata=true attribute (which I don't recommend) then the test will not be able to access this data. You would need to create the custom setting record within your test, or inside a @testsetup method. – Mark Keats Mar 4 '15 at 13:30
  • 1
    @MarkKeats that's it. feel free to post as an answer. – Saariko Mar 4 '15 at 13:43
4

Unless you specify the seealldata=true attribute (which I don't recommend) then the test will not be able to access this data. You would need to create the custom setting record within your test, or inside a @testsetup method.

  • This is allbit misleading. Even that we are referencing "variables" which one would consider that are accessible in @test methods. You still need to create the custom object in your test. Note, that if you refer to a specific dataset in your code, you will need to keep the same name in your code (test code and production code) – Saariko Mar 4 '15 at 16:33
1

My advice is to handle this within a test utility class, that creates an instance of the custom settings,. Then your test code can call that class to setup the custom Setting. Either that, or create it manually within each test class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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