You need to sign in to do that
Don't have an account?
Matthew Groesser
System.nullPointerException on afterInsert
HI all,
I have a simple brief to insert a contract on an account once the account has been created while using a factory trigger. I am getting a AccountTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object () error when trying to create an Account.
Here is my ITrigger interface:
public class TriggerException extends Exception {}
AccountGateway class:
I have a simple brief to insert a contract on an account once the account has been created while using a factory trigger. I am getting a AccountTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object () error when trying to create an Account.
Here is my ITrigger interface:
public interface ITrigger { void bulkAfter(); //For records inserted during a AFTER trigger void afterInsert(SObject so); void andFinally(); }Account Handler:
public with sharing class AccountHandler implements ITrigger { private Set<Id> cont_inUseIds = new Set<Id>(); private List<Contract> contr = new List<Contract>(); public AccountHandler() { } public void bulkAfter() { if(Trigger.isInsert){ cont_inUseIds = AccountGateway.findAccountIdsInUse(Trigger.oldMap.keySet()); } } public void afterInsert(SObject so){ Account myAccount = (Account)so; Contract contrTemp = new Contract(); contrTemp.Name = myAccount.Name; contrTemp.id = myAccount.Id; contr.add(contrTemp); } public void andFinally(){ if(!contr.isEmpty()){ insert contr; } } }Trigger Factory:
public with sharing class TriggerFactory{ public static void createHandler(Schema.sObjectType objType){ ITrigger handler = getHandler(objType); if (handler == null){ throw new TriggerException('no registered handler for object type '+ objType); } execute(handler); } private static void execute (ITrigger handler){ if(Trigger.isAfter){ handler.bulkAfter(); if(Trigger.isInsert){ for(SObject so : Trigger.new){ handler.afterInsert(so); } } //Actions for after delete and update would go here } handler.andFinally(); } public static ITrigger getHandler(Schema.sObjectType objType){ if (objType == Account.sObjectType){ return new AccountHandler(); } return null; } }The trigger itself:
trigger AccountTrigger on Account ( after insert){ TriggerFactory.createHandler(Account.sObjectType); }and the additional supporting classes:
public class TriggerException extends Exception {}
AccountGateway class:
public without sharing class AccountGateway { public static Set<Id> findAccountIdsInUse(Set<Id> accIds) { Set<Id> inUseIds = new Set<Id>(); for (Account[] accounts : [Select p.Id, (Select Id From Contracts Limit 1) From Account p where p.Id in : accIds]) { for (Account acc : accounts) { if (acc.Contracts.size() > 0) { inUseIds.add(acc.id); } } } return inUseIds; } }
I found my issue, it was quite simple and the problem was the way in which I was trying to reference inside the accountHandler class as well as the use of the account gateway. The correct code can be seen below: And of course the exception class:
public class TriggerException extends Exception {}
All Answers
There is a typo or an error at least for : contrTemp.id = myAccount.Id;
I saw the error and changed it, still getting the same error unfortunately.
I found my issue, it was quite simple and the problem was the way in which I was trying to reference inside the accountHandler class as well as the use of the account gateway. The correct code can be seen below: And of course the exception class:
public class TriggerException extends Exception {}