You need to sign in to do that
Don't have an account?

Apex Test Classes: "Attempt to de-reference a null object" error
Hi, I wrote a trigger and a test class, but when testing I get below error:
System.DmlException: Update failed. First exception on row 0 with id 003M000000Zc1NaIAJ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PopulateContactAreaCode: execution of BeforeUpdate
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.PopulateContactAreaCode: line 5, column 1: []
Trigger:
test class:
any idea what I added wrong?
thank you in advance
System.DmlException: Update failed. First exception on row 0 with id 003M000000Zc1NaIAJ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PopulateContactAreaCode: execution of BeforeUpdate
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.PopulateContactAreaCode: line 5, column 1: []
Trigger:
trigger PopulateContactAreaCode on Contact (before insert, before update) { for(Contact contact : Trigger.new) { string AreaCode = CountryRegion__c.getInstance(contact.MailingCountry).Area_Code__c; contact.Area_Code__c = AreaCode; } }
test class:
@isTest public class PopulateContactAreaCodeTest { static testMethod void attTriggerTest1() { test.startTest(); Account acct = new Account(id = '001M000000iLhTL', Name = 'Test Account ', Mailing_Country__c = 'Afghanistan'); update acct; Contact con = new Contact(id = '003M000000Zc1Na', LastName = 'Test Contact', Account = acct, Email = 'test@test.com', Mailing_Country__c = 'Afghanistan', MailingCountry = 'Afghanistan'); update con; delete con; test.stopTest(); } }
any idea what I added wrong?
thank you in advance
Because you're running a unit test, it doesn't see any data, and therefore no custom settings exist. You'll need to insert the custom setting before you insert the Contact record. Here's an example:
All Answers
Because you're running a unit test, it doesn't see any data, and therefore no custom settings exist. You'll need to insert the custom setting before you insert the Contact record. Here's an example:
Error: Compile Error: Variable does not exist: CountryRegion__c at line 5 column 27
I have the below trigger and trigger handler could you help me as I am getting two errors:
1. Attempt to derefrence a nullObject.
2. Assertion Failed.
if(Trigger.isAfter && Trigger.isInsert){
IFB_PayInSlipTriggerHandler.IncreasePISBalanceoncreation(Trigger.New);
}
Handler Class:
public static void IncreasePISBalanceoncreation(list<IFB_PayInSlips__c> payInSlips){
for(IFB_PayInSlips__c ObjPIS: payInSlips){
//check the status if it is PIScreated
IFB_CashVaultService.MaintainVaultBalance(ObjPIS.IFB_Status__c , ObjPIS.IFB_TotalAmount__c, ObjPIS.OwnerId);
}
and have again we have service class which is calling MaintainVaultBalance.
Let me know if any further details are required.
Thanks in advance.