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
Pankaj KPankaj K 

URGENT:{System.NullPointerException: Attempt to de-reference a null object:}

Thanks in advance..

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger Chk_Unique_Email caused an unexpected exception, contact your administrator: Chk_Unique_Email: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Chk_Unique_Email: line 62, column 9

 

I am getting this error when I'm trying to enter a new lead in Lead Object. Though it should give error that this email id already exist that what i have done in trigger but it's showing this error and strange thing is it's showing this error for a single user only, when trying to enter data for another user whos email id already exist it shows normal error. Can any one please explain me why.??

 

Do also tell me if i need any change in trigger..

trigger Chk_Unique_Email on Lead (before insert,before update) 
{
  Map<String, Lead> PrgEid = new Map<String, Lead>();
    for(Lead led:System.Trigger.new)
    {
        if((led.PRG_Employee_ID__c!=null) && (led.PRG_Employee_ID__c!=''))
        {
          if(Trigger.isInsert) 
            {
              String AccountID = led.PRG_Company__c;
              Account[] SalesForceAccunt = [Select Name from Account where ID=:AccountID];
              for(Account Acc:SalesForceAccunt)
              {
                String AccName = Acc.Name;
                AccName += ' ' + led.PRG_Employee_ID__c;
                PrgEid.put(AccName,led);
              }
             }
            if(Trigger.isUpdate)
            {
              if(led.PRG_Employee_ID__c != System.Trigger.oldMap.get(led.Id).PRG_Employee_ID__c)
                {
                  String AccountID = led.PRG_Company__c;
                Account[] SalesForceAccunt = [Select Name from Account where ID=:AccountID];
                for(Account Acc:SalesForceAccunt)
                {
                  String AccName = Acc.Name;
                  AccName += ' ' + led.PRG_Employee_ID__c;
                  PrgEid.put(AccName,led);
                }
              }
      }
        }
    }
    if(PrgEid.size()>0)
    {
      Lead[] ExistLead = [select ID, PRG_Company__c, PRG_Employee_ID__c, PRG_Unique_Identifier__c, Name from Lead where PRG_Unique_Identifier__c in : PrgEid.keySet() and IsConverted=false and IsDeleted=false limit 1000];
      if(ExistLead.size()>0)
      {
        for(Lead CheckLead:ExistLead)
        {
          Lead MapLead = PrgEid.get(CheckLead.PRG_Unique_Identifier__c);
          if(CheckLead.Id != MapLead.Id)
          {
            MapLead.PRG_Employee_ID__c.addError('Employee ID is exist for lead: <b><a href="/'+CheckLead.Id+'"/>'+CheckLead.Name+'</a></b>');
            break;
          }
        }
      }
      else
      {
        //For Sandbox
        //Contact[] ExistContact = [select ID, PRGUnique_Emp_ID__c, Name from Contact where PRGUnique_Emp_ID__c in : PrgEid.keySet() and IsDeleted=false and Rec_Typ__c='01270000000DVFp' limit 1000];
        //Contact[] ExistContact = [select ID, PRGUnique_Emp_ID__c, Name from Contact where PRGUnique_Emp_ID__c in : PrgEid.keySet() and IsDeleted=false and RecordTypeId='01270000000DVFpAAO' limit 1000];
        //For Production
        Contact[] ExistContact = [select ID, PRGUnique_Emp_ID__c, Name from Contact where PRGUnique_Emp_ID__c in : PrgEid.keySet() and IsDeleted=false and RecordTypeId='01270000000DVFpAAO' limit 1000];
        if(ExistContact.size()>0)
        {
          for(Contact CheckContact:ExistContact)
          {
            Lead MapLeadContact = PrgEid.get(CheckContact.PRGUnique_Emp_ID__c);
            MapLeadContact.PRG_Employee_ID__c.addError('Employee ID is exist for Contact: <b><a href="/'+CheckContact.Id+'"/>'+CheckContact.Name+'</a></b>');
            break; 
          }
        }
      } 
    }
}

 


ajmerakavitaajmerakavita

Hi

You might be getting this error because your map does not contain the value you are trying to retrieve.

First try checking if map has the value highlighted below.

 

Eg.

 

if(ExistContact.size()>0)
        {
          for(Contact CheckContact:ExistContact)
          {
   if(PrgEid.containsKey(CheckContact.PRGUnique_Emp_ID__c))
   {
    Lead MapLeadContact = PrgEid.get(CheckContact.PRGUnique_Emp_ID__c);
    MapLeadContact.PRG_Employee_ID__c.addError('Employee ID is exist for Contact: <b><a href="/'+CheckContact.Id+'"/>'+CheckContact.Name+'</a></b>');
    break;
   }
          }
        }

Pankaj KPankaj K

Thanks for the reply.

 

I have checked every query is returning value means it cannot be NULL.

Pankaj KPankaj K

This problem is not resolved yet. Can anbody tell how to solve this error

crop1645crop1645

Perhaps the issue is that the code you posted didn't highlight line 62 correctly and the problem is really here:

 

Lead MapLeadContact = PrgEid.get(CheckContact.PRGUnique_Emp_ID__c);
MapLeadContact.PRG_Employee_ID__c.addError('Employee ID is exist for Contact: <b><a href="/'+CheckContact.Id+'"/>'+CheckContact.Name+'</a></b>');

 The latter statement will throw the de-reference null object if MapLeadContact is null which could easily happen if the lookup on the prior ststement in the map returns null.

 

1. These problems are easily solved by inserting System.debug() statements into the code.  As much as it might seem like you're reverting to skills learned in BASIC programming, system.debug is really helpful

 

2.  Your commenting technique to handle record_type_id across orgs is not best practice.  You should do up at the top of the trigger a SOQL call something like this:

RecordType rt :	[select id, name, developerName from RecordType where developerName = 'myrecordtypename'];

 DeveloperName is invariant between orgs.  Then use rt.id in your SOQL as in:

 

Contact[] ExistContact = [select ID, PRGUnique_Emp_ID__c, Name from Contact where PRGUnique_Emp_ID__c in : PrgEid.keySet() and IsDeleted=false and RecordTypeId=:rt.id limit 1000];

 3. You can also simplify the code by doing this:

 

for (Contact checkContact: [select ID, PRGUnique_Emp_ID__c, Name from Contact where PRGUnique_Emp_ID__c in : PrgEid.keySet() and IsDeleted=false and RecordTypeId=:rt.id limit 1000]) {
	... your code for each contact to check ...
}