1

I'm trying to write a test class for my controller extension. In this extension I used addFields to add custom fields which i already filter with !Test.isRunningTest(), so it wont cause an error during the Testrun. The problem is I actually need the fields in the logic.

The if clause I use:

if(methodNum == '1' && header.TP_Kopf_1_Zentrierung__c  + header.TP_Kopf_1__c.length() + spaceShift < 68)

The test comes up to the if, but only throws a 'Attempt to de-reference a null object' afterwards.

My Extension Constructor:

   public ChangeSpacingController(ApexPages.StandardController stdController){
        if(!Test.isRunningTest()) stdController.addFields(new List<String>{'TP_Kopf_1__c','TP_Kopf_1_Zentrierung__c','TP_Kopf_2__c','TP_Kopf_2_Zentrierung__c','TP_Kopf_3__c','TP_Kopf_3_Zentrierung__c','TP_Kopf_4__c','TP_Kopf_4_Zentrierung__c','TP_Kopf_5__c','TP_Kopf_6__c','TP_Kopf_6_Zentrierung__c','Zeilen_zentrieren__c'});
        this.header = (TP_Header__c)stdController.getRecord();
    }

Current state of Testclass:

PageReference tpageRef = Page.TPKopf_ChangeSpacing;
        Test.setCurrentPage(tpageRef);

Account testAcc = new Account(Name = 'TestAccount', ao_Invoice_Type__c = 'Test', ax_lob__c = 'Test');
    insert testAcc;
    TP_Header__c testHeader = new TP_Header__c(Name = 'TestHeader', 
                                               TP_Kopf_1__c = 'Test1', 
                                               TP_Kopf_2__c = 'Test2', 
                                               TP_Kopf_3__c = 'Test3', 
                                               TP_Kopf_4__c = 'Test4', 
                                               TP_Kopf_5__c = 'Test5', 
                                               TP_Kopf_6__c = 'Test6',
                                               Zeilen_zentrieren__c = false,
                                               account__c = testAcc.ID);
    insert testHeader;

    ApexPages.StandardController sc = new ApexPages.StandardController(testHeader);

    ChangeSpacingController spController = new ChangeSpacingController(sc);
    spController.methodNum = '1';
    spController.addFive();

I also tried to insert the addFields into the Testclass itself which only gave me the error:

'You cannot call addFIelds when the data is being passed into the controller by the caller.'

3
  • 1
    At which line do you get 'Attempt to de-reference a null object' ?
    – kurunve
    Commented May 3, 2017 at 14:19
  • 1
    I think TP_Kopf_1_Zentrierung__c is the problem.
    – sfdcfox
    Commented May 3, 2017 at 14:22
  • @kurunve Line 176, which is the if
    – Tim Werner
    Commented May 3, 2017 at 14:28

1 Answer 1

0

Alright, after sfdcfoxs answer I checked the values of all fields and TP_Kopf_1_Zentrierung__c, was set to null. I thought that wouldn't be possible because it had a standard value of 1 set.

I just filled it in the test and is working now.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .