function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mxalix258mxalix258 

Attemp to de-reference a null object

I'm getting a null pointer exception on the line shown below in this trigger when doing bulk uploads, but seems to work fine in one off cases...anyone have a guess as to why?

 

trigger rolerequestTrigger on Application__c (before insert) 
{
    Set<Id> CanIds = new Set<Id>();

    Map<Id, KAP_Portal_Candidates__c> portalcan = new Map<Id, KAP_Portal_Candidates__c>();

    for (Application__c app : Trigger.new) 
    {
       if (app.RecordTypeId == '012C0000000Bhsr' && app.Program__c == 'KAP 2013-14'){
        canIds.add(app.Contact__c);
    }
    }
    if(canIds.size() > 0)
    {
        for(KAP_Portal_Candidates__c kpc : [SELECT Id, Associated_Contact__c FROM KAP_Portal_Candidates__c WHERE Associated_Contact__c IN :canIds])
        {
            portalcan.put(kpc.Associated_Contact__c , kpc);
        }
    if(portalcan.size() > 0)
    {
        for (Application__c app : Trigger.new)
        {

Id kapID = portalCan.get(app.Contact__c).Id;

            if(kapId != null)
            {
               app.KAP_Portal_Candidate__c = kapId;
               }   
            }
        }
    }
    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;
               }   
            }
        }
    }
}

 

Shailesh DeshpandeShailesh Deshpande
It indicates that in the map, for the key app.contact, the value is null. .I.e is for one/some of the records,
portalCans.get(app.Contact__c) is null. Thus when you try to refer portalCans.get(app.Contact__c).Id, it throws the error.
mxalix258mxalix258

It shouldn't be the case, because in this example all application records have an associated candidate record. I can't figure out what would be causing that error....

Jeff MayJeff May

It can happen, since in the top loop, you are only adding acc.Contacts to the list under certain conditions. In the bottom loop you are looking up Contacts for all accounts.

mxalix258mxalix258

Do you have a recommendation on what I should change to avoid that situation? The purpose of the trigger is that when an application is created that meets a certain criteria it will search to find an adjacent record in another object that is connected to the same contact record. Once it is found, it should update a lookup on the application with that other object record.

 

Thanks for all the help so far.

Jeff MayJeff May

you can just check to make sure the map has the key before trying to de-reference it:

 

if (theMap.containsKey(acc.Contact)) {

 

}