+ Start a Discussion
mxalix258mxalix258 

Attempt to dereference a null object

I cannot figure out why I am receiving this null pointer exception on line 23. I thought it would be similar to this article: http://cloudnow.wordpress.com/2011/04/04/apex-bloopers-why-art-thou-null/ but the code seems correct to me. The code works when creating single records in the database, but when i try to bulk upload, it fails with the null pointer exception. Any ideas?

 

trigger kiprolerequestTrigger on Application__c (after insert) 
{
    Set<Id> KIPCanIds = new Set<Id>();

    Map<Id, KIP_Candidates__c> kipportalcan = new Map<Id, KIP_Candidates__c>();

    for (Application__c app : Trigger.new) 
    {
       if (app.RecordTypeId == '012C0000000Bhsr' && app.Program__c == 'KIP Summer 2013'){
        KIPcanIds.add(app.Contact__c);
    }
    }
    if(KIPcanIds.size() > 0)
    {
        for(KIP_Candidates__c kippc : [SELECT Id, Associated_Contact__c FROM  KIP_Candidates__c WHERE Associated_Contact__c IN :KIPCanIds])
        {
            kipportalcan.put(kippc.Associated_Contact__c , kippc);
        }
    if(kipportalcan.size() > 0)
    {
        for (Application__c app : Trigger.new)
        {
            Id kipId = kipportalCan.get(app.Contact__c).Id;

            if(kipId <> null)
            {
               app.KIP_Portal_Candidate__c = kipId;
               }   
            }
        }
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
KaminiKamini

Please try the following code:

 

 

trigger kiprolerequestTrigger on Application__c (after insert)
{
Set<Id> KIPCanIds = new Set<Id>();

Map<Id, KIP_Candidates__c> kipportalcan = new Map<Id, KIP_Candidates__c>();

for (Application__c app : Trigger.new)
{
if (app.RecordTypeId == '012C0000000Bhsr' && app.Program__c == 'KIP Summer 2013'){
KIPcanIds.add(app.Contact__c);
}
}
if(KIPcanIds.size() > 0)
{
for(KIP_Candidates__c kippc : [SELECT Id, Associated_Contact__c FROM KIP_Candidates__c WHERE Associated_Contact__c IN :KIPCanIds])
{
kipportalcan.put(kippc.Associated_Contact__c , kippc);
}
if(kipportalcan.size() > 0)
{
for (Application__c app : Trigger.new)
{
if(app.contact__c != null && kipportalCan.get(app.Contact__c) != null)
app.KIP_Portal_Candidate__c = kipportalCan.get(app.Contact__c).id;
}
}
}
}

All Answers

dphilldphill

It's because your map isn't brining back a value.  Make your map directly bring back an Id instead.

Map<Id, Id> kipportalcan = new Map<Id, Id>();
kipportalcan.put(kippc.Associated_Contact__c, kippc.Id);
Id kipId = kipportalCan.get(app.Contact__c);

 

 

 

 

KaminiKamini

Please try the following code:

 

 

trigger kiprolerequestTrigger on Application__c (after insert)
{
Set<Id> KIPCanIds = new Set<Id>();

Map<Id, KIP_Candidates__c> kipportalcan = new Map<Id, KIP_Candidates__c>();

for (Application__c app : Trigger.new)
{
if (app.RecordTypeId == '012C0000000Bhsr' && app.Program__c == 'KIP Summer 2013'){
KIPcanIds.add(app.Contact__c);
}
}
if(KIPcanIds.size() > 0)
{
for(KIP_Candidates__c kippc : [SELECT Id, Associated_Contact__c FROM KIP_Candidates__c WHERE Associated_Contact__c IN :KIPCanIds])
{
kipportalcan.put(kippc.Associated_Contact__c , kippc);
}
if(kipportalcan.size() > 0)
{
for (Application__c app : Trigger.new)
{
if(app.contact__c != null && kipportalCan.get(app.Contact__c) != null)
app.KIP_Portal_Candidate__c = kipportalCan.get(app.Contact__c).id;
}
}
}
}

This was selected as the best answer
mxalix258mxalix258
thank you!