Apex Test Class throwing NullPointerException

apexcode-coveragefailing-tests

UPDATE

  • NullPointerException error has been resolved (thanks @Adrian)
  • INVALID_PERSON_ACCOUNT_OPERATION error was resolved by adding a RecTypeId variable
  • Now I am confronted by an insert error: REQUIRED_FIELD_MISSING, Required fields are missing: [ContactId]: [ContactId] (stack trace is Class.lifeEventNotifyControllerTest.TestSOQL: line 24, column 1)

Since I am looking at the Person Account, dummyContact.Id isn't an option, so I tried dummyContact.PersonContactId but that threw the same error. What am I missing?

Updated Apex Test

@isTest
private class lifeEventNotifyControllerTest { 

    static testMethod void TestSOQL() {
        String loggedUserId = UserInfo.getUserId();
        String RecTypeId = [SELECT Id FROM RecordType WHERE (Name='Person Account') AND (SobjectType='Account')].Id;
        
        Account dummyContact = new Account();
        dummyContact.RecordTypeID = RecTypeId;
        dummyContact.Firstname = 'Test';
        dummyContact.Lastname = 'Contact';
        dummyContact.PersonBirthdate = date.today();
        dummyContact.Contact_Type__pc = 'Sponsor';
        INSERT dummyContact;

        Account dummyAcct = new Account();
        dummyAcct.Name = 'Test Company';
        dummyAcct.Custom__c = loggedUserId;
        INSERT dummyAcct;

        AccountContactRelation acr = new AccountContactRelation();
        acr.ContactId = dummyContact.PersonContactId;
        acr.AccountId = dummyAcct.Id;
        INSERT acr;

        lifeEventNotifyController testCont = new lifeEventNotifyController ();
        List<AccountContactRelation> dummyList = testCont.getLifeEvents();

        System.assert(dummyList  != null);
        
    }
}

Original Question:

I am brand new to Apex development and am learning about the importance of code coverage. Right now I have a very simple Apex class querying the AccountContactRelation object via SOQL. When trying to create an Apex test class, I continue to get the following error:

"System.NullPointerException: Attempt to de-reference a null object" error.

Browsing StackExchange I believe this error has to do with my Account and Contact objects not being initialized in the test class?

Apex Class

public with sharing class lifeEventNotifyController {
  @AuraEnabled
  public List<AccountContactRelation> getLifeEvents() {
    // get current user id
    String loggedUserId = UserInfo.getUserId();
    List<AccountContactRelation> lifeEventList = [SELECT Id, ContactId, Contact.Name, Contact.Birthdate, AccountId, Account.Name FROM AccountContactRelation WHERE Account.Custom__c = :loggedUserId AND (Contact.Days_to_Birthday__c <= 7 OR Contact.Days_to_Birthday__c = 365)];
    
    return lifeEventList;

  }
}

NOTE: In the above class, Days_to_Birthday__c is a formula field based on the Contact.Birthdate date field.

Apex Test Class

@isTest
private class lifeEventNotifyControllerTest { 

    static testMethod void TestSOQL() {
        String loggedUserId = UserInfo.getUserId();
        
        Contact dummyContact = new Contact();
        dummyContact.Firstname = 'Test';
        dummyContact.Lastname = 'Contact';
        dummyContact.Birthdate = date.today();
        INSERT dummyContact;

        Account dummyAcct = new Account();
        dummyAcct.Name = 'Test Company';
        dummyAcct.Custom__c = loggedUserId;
        INSERT dummyAcct;

        AccountContactRelation acr = new AccountContactRelation();
        acr.ContactId = dummyContact.Id;
        acr.Contact.Birthdate = dummyContact.Birthdate;
        acr.AccountId = dummyAcct.Id;
        acr.Account.Name = dummyAcct.Name;
        acr.Account.Custom__c = dummyAcct.Custom__c;
        INSERT acr;

        lifeEventNotifyController testCont = new lifeEventNotifyController ();
        List<AccountContactRelation> dummyList = testCont.getLifeEvents();

        System.assert(dummyList  != null);
        
    }
}

Best Answer

Your problem seems to be in how you set the AccountContactRelationship.AccountId lookup. Specifically, you are trying to set both that field (which you do correctly), but also set Account.Name and Account.Custom__c, which are incorrect. Simply remove the lines which set those fields, as they appear to be the only possible source of any NullPointerException.

Related Topic