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
kcharubinkcharubin 

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

Hello I am a beginner developer. I have implemented an apex trigger that counts all related Consumer Lead records.

I am getting this error when I try to create a new Lead Request record

 

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

 

trigger rollupConsumerLeads on Lead_Request__c (before insert, before update) {
  for(Lead_Request__c p:Trigger.new)
    p.Leads_Given_NEW__c = 0;
  for(Consumer_Leads__c c:[select id,Lead_Request_NEW__c from Consumer_Leads__c where Lead_Request_NEW__c in :Trigger.new])
    Trigger.newMap.get(c.Lead_Request_NEW__c).Leads_Given_NEW__c++;
}

 

Any help would be appricciated.

Best Answer chosen by Admin (Salesforce Developers) 
kcharubinkcharubin

After some researching and trial and error I found a solution to my problem.

The querry was returning a null value for some reason causing a System.NullPointerException.

To fix the issue I surrounded that part of code withe a try catch statement.

Here is the working code.

 

trigger rollupConsumerLeads on Lead_Request__c (before insert, before update) {
  for(Lead_Request__c p:Trigger.new){
    p.Leads_Given_NEW__c = 0;
    }
  for(Consumer_Leads__c c:[select id,Lead_Request_NEW__c from Consumer_Leads__c where Lead_Request_NEW__c in :Trigger.new ]){
    try{
        Trigger.newMap.get(c.Lead_Request_NEW__c).Leads_Given_NEW__c++;
    }
    catch(System.NullPointerException e){  
        Return;
    }
  }
}

 When it finds a null value it ignores it

All Answers

kcharubinkcharubin

After some researching and trial and error I found a solution to my problem.

The querry was returning a null value for some reason causing a System.NullPointerException.

To fix the issue I surrounded that part of code withe a try catch statement.

Here is the working code.

 

trigger rollupConsumerLeads on Lead_Request__c (before insert, before update) {
  for(Lead_Request__c p:Trigger.new){
    p.Leads_Given_NEW__c = 0;
    }
  for(Consumer_Leads__c c:[select id,Lead_Request_NEW__c from Consumer_Leads__c where Lead_Request_NEW__c in :Trigger.new ]){
    try{
        Trigger.newMap.get(c.Lead_Request_NEW__c).Leads_Given_NEW__c++;
    }
    catch(System.NullPointerException e){  
        Return;
    }
  }
}

 When it finds a null value it ignores it

This was selected as the best answer
sfdcfoxsfdcfox
That doesn't answer the "why" though. Why you got it is because you were trying to access a value where Lead_Request_NEW__C was null-- so there was no record to look up. FYI, you cannot use Trigger.newMap in a before insert trigger, because they do not yet have an ID assigned to them.
TechnozChampTechnozChamp
Use the Null Condition Check in the code so that rest of the code is executed in Try Block.

http://technozchamps.blogspot.com/2015/02/caused-by-systemnullpointerexception.html