+ Start a Discussion
JN22JN22 

Attempt to de-reference a null object error

I have a trigger that fires on a custom object upon insert, update, or deletion.  The trigger updates the Associated Account record, but only if certain conditions are met.  It works fine when the conditions are met, but when they are not, I get the following error:

 

Apex trigger Contract2Account caused an unexpected exception, contact your administrator: Contract2Account: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Contract2Account: line 23, column 1

 

 

My trigger code is below:

 

trigger Contract2Account on Contract_Summary__c (after insert, after update, after delete)
{

Map<Id,Account> oppMap = new Map<Id,Account>();
Set<id> Ids = new Set<id>();

if(trigger.isInsert || trigger.isInsert)
{

for (Contract_Summary__c prgm : Trigger.new)
{
if(prgm.Type_of_Contract__c == 'Renewal' || prgm.Type_of_Contract__c == 'Initial MSA')
Ids.add(prgm.Account_Name__c);
}

Map<id,Account> acctMap2 = new Map<id,Account>([Select Id,Name,Contract_Summary__c from Account Where Id in :Ids]);

for (Contract_Summary__c prgm2 : Trigger.new)
{
if(prgm2.Account_Name__c != null)
{
Account a = acctMap2.get(prgm2.Account_Name__c);
a.Contract_Summary__c = prgm2.Id;

oppMap.put(a.id,a);
}
}
update oppMap.values();
}
if(trigger.isDelete)

{

for (Contract_Summary__c prgm : Trigger.old)
{
if(prgm.Type_of_Contract__c == 'Renewal' || prgm.Type_of_Contract__c == 'Initial MSA')
Ids.add(prgm.Account_Name__c);
}

Map<id,Account> acctMap2 = new Map<id,Account>([Select Id,Name,Contract_Summary__c from Account Where Id in :Ids]);

for (Contract_Summary__c prgm2 : Trigger.old)
{
if(prgm2.Account_Name__c != null)
{

Account a = acctMap2.get(prgm2.Account_Name__c);
a.Contract_Summary__c = null;

oppMap.put(a.id,a);

}
}
update oppMap.values();
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Andrew WilkinsonAndrew Wilkinson

Your update statement I believe is at line 23. Put it in a try catch statement to catch the error instead of not handling it. The issue is the map is null with no values. But your update statement should be caught. You can use any one of these catches or all. The NullPointerException is the one you are looking for. The general Exception would catch this but if you wanted it to be specific.

try{

update oppMap.values()

}

catch(DMLException dmle){

//some logic

}

catch(NullPointerException npe){

//some code

}

catch(Exception e){

//some other logic

}

All Answers

Andrew WilkinsonAndrew Wilkinson

Your update statement I believe is at line 23. Put it in a try catch statement to catch the error instead of not handling it. The issue is the map is null with no values. But your update statement should be caught. You can use any one of these catches or all. The NullPointerException is the one you are looking for. The general Exception would catch this but if you wanted it to be specific.

try{

update oppMap.values()

}

catch(DMLException dmle){

//some logic

}

catch(NullPointerException npe){

//some code

}

catch(Exception e){

//some other logic

}

This was selected as the best answer
kirkevonphillykirkevonphilly

Does your Account_Name__c field hold ID values?  

 

If not, then this line seems fishy:

Account a = acctMap2.get(prgm2.Account_Name__c);

You defined acctMap2 as <id,account> which is populated by your query.

 

So unless your prgrm2.Account_Name__c is storing an ID, the map won't contain the name and won't return a value.

 

 

JN22JN22

Andrew,

 

I am relatively new to Apex coding and have not used Try Catch before.  From what I can gather, isn't this designed to catch and exception and then return an error message or something?  In my case, I want to make it so the trigger simply does not fire when the conditions are met.  I don't want any messages to display as there is no error being made by the user.

JN22JN22

Kirk,

 

prgrm2.Account_Name__c is a lookup field on my custom object.  Doesn't that hold ID values bu the nature of the field?  Thanks,

kirkevonphillykirkevonphilly

Is your Account a being populated?  Can you throw a system.debug before this line to ensure your map contains the key?

 

Let's see what the  results of these sys debugs are:

system.debug('Account Name:  ' + prgrm2.Account_Name__c);
system.debub('Map contains: ' + acctmap2.size());
system.debug('Map Value: ' + acctMap2.get(prgm2.Account_Name__c));
Account a = acctMap2.get(prgm2.Account_Name__c);
JN22JN22

Thanks for all the input.  I got the trigger to work using a Try-Catch statement and some conditions.  Below is the final code:

 

trigger Contract2Account on Contract_Summary__c (after insert, after update, after delete)
{
Try
{
Map<Id,Account> oppMap = new Map<Id,Account>();
Set<id> Ids = new Set<id>();

if(trigger.isInsert || trigger.isUpdate)
{

for (Contract_Summary__c prgm : Trigger.new)
{
if(prgm.Type_of_Contract__c == 'Renewal' || prgm.Type_of_Contract__c == 'Initial MSA')
Ids.add(prgm.Account_Name__c);
}

Map<id,Account> acctMap2 = new Map<id,Account>([Select Id,Name,Contract_Summary__c, Contract_Summary__r.Current_Effective_Date__c from Account Where Id in :Ids]);

for (Contract_Summary__c prgm2 : Trigger.new)
{
if(acctMap2.containsKey(prgm2.Account_Name__c) && (acctMap2.get(prgm2.Account_Name__c).Contract_summary__r.Current_Effective_Date__c < prgm2.Current_Effective_Date__c || acctMap2.get(prgm2.Account_Name__c).Contract_summary__r.Current_Effective_Date__c == null)) {

Account a = acctMap2.get(prgm2.Account_Name__c);
a.Contract_Summary__c = prgm2.Id;

oppMap.put(a.id,a);
}
}
update oppMap.values();
}


if(trigger.isDelete)
{

for (Contract_Summary__c prgm : Trigger.old)
{
if(prgm.Type_of_Contract__c == 'Renewal' || prgm.Type_of_Contract__c == 'Initial MSA')
Ids.add(prgm.Account_Name__c);
}

Map<id,Account> acctMap2 = new Map<id,Account>([Select Id,Name,Contract_Summary__c from Account Where Id in :Ids]);

for (Contract_Summary__c prgm2 : Trigger.old)
{
Account a = acctMap2.get(prgm2.Account_Name__c);
a.Contract_Summary__c = null;

oppMap.put(a.id,a);
} update oppMap.values();
}
}

catch(Exception e)
{}

}