• Matthew Groesser
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
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 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;
	}
}





 
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 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;
	}
}