Skip to main content
I have getting error as System.NullPointerException: Attempt to de-reference a null object on the test class

Class:-

/*

@Class Description- Contact Trigger Helper.

Class is introduced to handle Titan Portal Status Sync between Contact and its related account.

*/ public with Sharing class ContactHelper {

//Boolean variable to prevent recursions on this Class.

public static Boolean isPortalSyncUpdateRecursion = FALSE ; public static Boolean isPortalSyncDeleteRecursion = FALSE ;

public void updateAccountPortalStatus(Map<Id,Contact> mapNewContact, Map<Id,Contact> mapOldContact) { system.debug('mapNewContact' +mapNewContact ); system.debug('mapOldContact' +mapOldContact );

Set<Id> setContactIds = new Set<Id>(); Set<Id> setAccountIds = new Set<Id>(); List<Account> lstAccountsToUpdate = new List<Account>(); if(mapNewContact != null) { for(Contact objContact : mapNewContact.values()) { system.debug('mapOldContact' +mapOldContact.get(objContact.Id).Titan_Portal_Status__c ); system.debug('objContact'+ objContact.Titan_Portal_Status__c); //if there is a change in Titan Portal Status of this contact, collect contact ids. if(mapOldContact != null && mapOldContact.containsKey(objContact.Id)) { if(objContact.Titan_Portal_Status__c != mapOldContact.get(objContact.Id).Titan_Portal_Status__c ) { setContactIds.add(objContact.Id);//collect contact ids that needs to be evaluated for all AccountContactRelation system.debug('setContactIdsinfor' + setContactIds); } } } } else if(Trigger.isDelete) { System.debug('Old Keyset'+mapOldContact.keySet()); for(AccountContactRelation objAccountContactRelation : [SELECT Id, AccountId FROM AccountContactRelation WHERE ContactID IN: mapOldContact.keySet()]) { //NOTE-use this variable only in Contact Delete context, as this is used to find accounts of deleted contact setAccountIds.add(objAccountContactRelation.AccountId); } } System.debug('Set Contact Ids:'+setContactIds); System.debug('Set Account Ids:'+setAccountIds); /* * NOTE: This blank update will execute the record-triggered flows in AccountContactRelation, which will handle * Titan Portal Status sync between Accounts and their related Contacts linked using AccountContactRelation. * */ if(setContactIds.size()>0 && Trigger.isUpdate) { system.debug('setContactIdsinupdate' + setContactIds); List<AccountContactRelation> lstToUpdate = [SELECT Id FROM AccountContactRelation WHERE ContactId IN: setContactIds]; if(lstToUpdate.size()>0) { Database.update(lstToUpdate,false); } } /* * NOTE: - For delete of contact, validate the Portal Flag in this method * */ if(setAccountIds.size()>0 && Trigger.isDelete) { Boolean portalFlag = FALSE; for(Account objAccount: [SELECT Id, Titan_Enabled__c , (SELECT Id, Contact_Titan_Portal_Status__c FROM AccountContactRelations WHERE Contact_Titan_Portal_Status__c ='Active' AND ContactId NOT IN: mapOldContact.keySet() LIMIT 1) FROM Account WHERE Id IN: setAccountIds]) { //For the deleted contact, There is atleast 1 more diff contact related to the account, which is portal active. if(objAccount.AccountContactRelations != null && objAccount.AccountContactRelations.size()>0) { portalFlag = TRUE; } else { portalFlag = FALSE; } System.debug('Portal Flag'+portalFlag); System.debug('Account Flag'+objAccount.Titan_Enabled__c); //if there is a change in Account checkbox field and the flag var calculated here, add this update for update if(objAccount.Titan_Enabled__c != portalFlag) { objAccount.Titan_Enabled__c = portalFlag; lstAccountsToUpdate.add(objAccount); } } System.debug('Accountupdate list:'+lstAccountsToUpdate); //Update the accounts' Titan Enabled checkbox if(lstAccountsToUpdate.size()>0) { Database.update(lstAccountsToUpdate, false); } }

} }

Test class:- @isTest public class ContactHelperTest { static testmethod void PortalStatus(){ Map<id,contact> oldmap = new Map<id,contact>(); Map<id,contact> newmap = new Map<id,contact>(); Account acct = new Account(); acct.Name='TEST'+ math.random(); acct.Type='Standard'; acct.BillingStreet='Test'; acct.BillingCity='test'; acct.BillingCountry='United States'; acct.Phone='123214231453'; acct.BillingPostalCode='1234567'; acct.BillingState='California'; acct.Titan_Enabled__c = True; acct.OwnerId=userinfo.getUserId(); //acct.Billing_Address__c = 'XYZ, Albama, ' insert acct;

system.debug('acct'+ acct); Contact con = new Contact(AccountId = acct.id,lastname = 'testdata' , firstname ='testdata1', Email = 'test1@honeywell.com'); insert con; system.debug('con'+ con); con.lastname = 'Test2'; con.Titan_Portal_Status__c = 'Active'; update con; system.debug('conupdate'+ con); oldmap.put(con.id,con); system.debug('oldmap'+ oldmap); contact con1 = [select id,name,lastname,firstname,email,Titan_Portal_Status__c from contact where id =:con.Id]; system.debug('con1' +con1); con1.lastname = 'Test3'; con1.Titan_Portal_Status__c = 'Inactive'; update con1; system.debug('con1update' +con1); newmap.put(con1.id,con1); system.debug('newmap' +newmap); AccountContactRelation all = [SELECT ID, AccountId, ContactId,Contact_Titan_Portal_Status__c, IsActive FROM AccountContactRelation where contactid =:con1.id]; system.debug('all' + all); contact con2 = [select id,name,lastname,firstname,email,Titan_Portal_Status__c from contact where id =:con1.Id]; delete con2; Test.startTest(); ContactHelper ts = new ContactHelper(); try{ ts.updateAccountPortalStatus(newmap,oldmap); } catch (DmlException ex) { System.assertEquals('expected text', ex.getMessage()); } Test.stopTest(); /* check for AccountContactRelation record with IsActive = false and also check for the error message */ }

System.NullPointerException: Attempt to de-reference a null object on test class

User-added image

Getting below error while running the test class

can anyone please help on this as we are getting error on the System.NullPointerException: Attempt to de-reference a null object on line 50 on the class "if(setContactIds.size()>0 && Trigger.isUpdate)"
1 个回答
  1. 2023年8月31日 10:40
    HI Mahesh,

    I see you also posted on https://salesforce.stackexchange.com/questions/402221/system-nullpointerexception-attempt-to-de-reference-a-null-object-on-test-class according to which, 

    "This test class is incoherent and does not appear to be meaningfully testing the class ContactHelper.

    It looks like you are attempting to test behavior on Contact deletion, but the logic you've written does not do so effectively for a number of reasons. A few points that I will highlight, which may not be the specific issue causing this error and may not be all of the issues in this code, are:

    > Your code appears to invoke the trigger handler multiple times. The handler will be run, presumably, by your trigger at each DML operation you perform during test data setup, and will then explicitly be invoked again when you call it. This is almost certainly not what you want. I assume you were trying to build a DML-free test to exercise this code in isolation, but you haven't actually done that - you're still performing lots of DML.

    > You have static Booleans to try to control recursion, but you are not using them. If in your actual code these values are used, you may see unexpected behavior because of the way your test runs the code multiple times in a single transaction.

    >You are attempting to catch and validate a DmlException that your code never causes to be thrown (by calling addError()).

    >Your test will false-positive if an exception is not thrown because you do not make a System.assert(false) assertion after you call ts.updateAccountPortalStatus(newmap,oldmap) to ensure that the flow of control doesn't continue.

    >The logic in your test will cause the wrong code path to be executed in your trigger handler. Because you construct and pass an incorrect newmap value to the handler (delete events do not have a Trigger.newMap), your handler will execute what appears to be the update pathway in the first if statement, instead of the delete pathway.

    > That said, the logic in your handler is so confused that it is difficult to follow the flow of control. You need to step back and do a major refactor, breaking out logic for different trigger events into separate methods and business logic into its own testable code unit."

    Related:

    https://developer.salesforce.com/forums/?id=906F0000000AvQJIA0

    https://stackoverflow.com/questions/70463065/salesforce-test-class-system-nullpointerexception-attempt-to-de-reference-a-n

    https://www.sfdcpoint.com/salesforce/system-nullpointerexception-attempt-to-de-reference-a-null-object/

    Please mark answer as best to close the thread. Thanks

     
0/9000