1 个回答
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=906F0000000AvQJIA0https://stackoverflow.com/questions/70463065/salesforce-test-class-system-nullpointerexception-attempt-to-de-reference-a-nhttps://www.sfdcpoint.com/salesforce/system-nullpointerexception-attempt-to-de-reference-a-null-object/Please mark answer as best to close the thread. Thanks