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

de-reference a null object
I have been trying to find a fix for the following error.
I am recieving the following ereror message when I test an apex class.
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdatePrimaryUnitOTCContact: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.UpdatePrimaryUnitOTCContact: line 29, column 30: [] and
Class.TestupdatePrimaryOTCcontact.testClass: line 14, column 9 External entry point.
here is the scenario,
when i am writing test class for UpdatePrimaryOTCContact trigger but i am getting an error in different Trigger(PrimaryUnitOTCContact).
Test Class:
here is the trigger that related to this class:
trigger UpdatePrimaryOTCContact on OTC_Contact__c (after insert, after update) {
List <String> ContactIds= new List<String>();
For(OTC_Contact__c otc : trigger.new)
{
ContactIds.add(otc.Contact__c);
}
List<Contact>Contacts = [Select Id, Name, Primary_OTC_Contact__c, Primary_CheckPoint_Ship_To__c, Primary_Checkpoint_Learning_Ship_To__c From Contact Where id IN:ContactIds];
Map<String,Contact>ContactMap = New Map<String,Contact>();
For(Contact ct : Contacts)
{
ContactMap.Put(ct.id, ct);
}
List<Contact> ConUpdates = New List<Contact>();
For(OTC_Contact__c otc : Trigger.new)
{
system.debug('Entered for loop');
if(otc.Is_Primary__c==TRUE && otc.Is_Primary_Checkpoint_Ship_To__c==TRUE
&& otc.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE)
{
Contact con = ContactMap.get(otc.Contact__c);
system.debug(con.Id);
con.Primary_OTC_Contact__c=otc.id;
con.Primary_CheckPoint_Ship_To__c=otc.id;
con.Primary_Checkpoint_Learning_Ship_To__c=otc.id;
ConUpdates.add(con);
}
else if(otc.Is_Primary__c==TRUE && otc.Is_Primary_Checkpoint_Ship_To__c==TRUE)
{
Contact con = ContactMap.get(otc.Contact__c);
system.debug(con.Id);
con.Primary_OTC_Contact__c=otc.id;
con.Primary_CheckPoint_Ship_To__c=otc.id;
ConUpdates.add(con);
}
else if(otc.Is_Primary_Checkpoint_Ship_To__c==TRUE && otc.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE)
{
Contact con = ContactMap.get(otc.Contact__c);
system.debug(con.Id);
con.Primary_CheckPoint_Ship_To__c=otc.id;
con.Primary_Checkpoint_Learning_Ship_To__c=otc.id;
ConUpdates.add(con);
}
else if(otc.Is_Primary__c==TRUE && otc.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE)
{
Contact con = ContactMap.get(otc.Contact__c);
system.debug(con.Id);
con.Primary_OTC_Contact__c=otc.id;
con.Primary_Checkpoint_Learning_Ship_To__c=otc.id;
ConUpdates.add(con);
}
else if(otc.Is_Primary__c==TRUE)
{
Contact con = ContactMap.get(otc.Contact__c);
system.debug(con.Id);
con.Primary_OTC_Contact__c=otc.id;
ConUpdates.add(con);
}
else if(otc.Is_Primary_Checkpoint_Ship_To__c==TRUE)
{
Contact con = ContactMap.get(otc.Contact__c);
system.debug(con.Id);
con.Primary_CheckPoint_Ship_To__c=otc.id;
ConUpdates.add(con);
}
else if(otc.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE)
{
Contact con = ContactMap.get(otc.Contact__c);
system.debug(con.Id);
con.Primary_Checkpoint_Learning_Ship_To__c=otc.id;
ConUpdates.add(con);
}
}
update ConUpdates;
}
here is the trigger that i am getting an error saying that De-reference null Object.
trigger UpdatePrimaryUnitOTCContact on OTC_Contact__c (after insert, after update) {
List <String> UnitIds= new List<String>();
For(OTC_Contact__c otc : trigger.new)
{
UnitIds.add(otc.Unit__c);
}
List<Unit__c>Units = [Select Id, Name, Primary_OTC_Contact__c From Unit__c Where id IN:UnitIds];
Map<String,Unit__c>UnitMap = New Map<String,Unit__c>();
For(Unit__c ut : Units)
{
UnitMap.Put(ut.id, ut);
}
List<Unit__c> UnitUpdates = New List<Unit__c>();
For(OTC_Contact__c otc : Trigger.new)
{
system.debug('Entered for loop');
if(otc.Is_Primary_Unit_Contact__c==TRUE)
{
Unit__c un = UnitMap.get(otc.Unit__c);
system.debug(un.Id);
un.Primary_OTC_Contact__c=otc.id;
UnitUpdates.add(un);
}
else
{
Unit__c un = UnitMap.get(otc.Unit__c);
system.debug(un.Id);
un.Primary_OTC_Contact__c=null;
UnitUpdates.add(un);
}
}
update UnitUpdates;
}
can anyone help with sample code for that error. i have to deploy that code into QA tommorow.
thanks in advance and waiting for reply.
Unit__c un
This object is null. You need to modify your test class so that it covers every condition in your trigger.
thanks for your reply,
i tried in that way but i could not able to solve that error.
i you how to do that can please provide me som sample code.,
thanks again.
Remove the code form your trigger. Put it in separate class inside a method. Call that method in your test class with different parameters.
Call the method in your trigger by passing the parameters.
i am new to this apex coding, i tried this from one past one week.
if you have any sample code can you please post it here.
thanks for response and thanks for helping out with this issue.