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
Alekhya Mathukumilli 7Alekhya Mathukumilli 7 

Attempt to de-reference a null object in Prod

my code works fine in UAT but when deployed in Production is throwing an error
Attempt to de-reference a null object 
it works fine with other two objects i have in the code only with one object. I tried checking everything I could that is linked up with this. 
All I am stuck is why is it working in UAT and not in Prod.

This is where I am getting an error in Prod
   if(!comlist.isEmpty()){
           for(Comliance_Checklist__c cl:comlist ){
             cidset.add(cl.Id);  
             if(cl.Category__c=='Outside/ Waste Area'){
               violationwrapper wr = new violationwrapper();
               //wr.violationid = cl.Id;
               wr.category  = cl.Category__c;
               wr.violationid = cl.ViolationId__c;  
               wr.violation = violationMap.get(cl.ViolationId__c).Violation__c;
               wr.guidance  = violationMap.get(cl.ViolationId__c).Guidance_to_Avoid_Violation__c ;
               wr.penalty   = violationMap.get(cl.ViolationId__c).Penalty__c;
               wr.notes     = cl.Notes__c;
               wr.topic     = cl.Topic_Discussed__c;
               dsnyList.add(wr);
               AllList.add(wr);
             }

it says al line line 70 and line 70 is my "}"

Please help me with this.
Best Answer chosen by Alekhya Mathukumilli 7
Dilip_VDilip_V
Hi Alekya,

You wrote these lines 
wr.violation = violationMap.get(cl.ViolationId__c).Violation__c;
wr.guidance  = violationMap.get(cl.ViolationId__c).Guidance_to_Avoid_Violation__c ;
 wr.penalty   = violationMap.get(cl.ViolationId__c).Penalty__c;

But you are not checking for key in the map.So it would be better if you add IF condition before getting values from the map.
 
if(cl.ViolationId__c!=null&&violationMap.containskey(cl.ViolationId__c))
{
wr.violation = violationMap.get(cl.ViolationId__c).Violation__c;
               wr.guidance  = violationMap.get(cl.ViolationId__c).Guidance_to_Avoid_Violation__c ;
               wr.penalty   = violationMap.get(cl.ViolationId__c).Penalty__c;
}
Thanks.
Please let me know if it helps.
 

All Answers

UC InnovationUC Innovation
Hi Alekhya,

So I see a couple possibilities, but it would be helpful if you post all the code to get more context.

From what I can tell, there's two spots that can cause a null dereferencing error. 

1) comlist is null
2) dsnyList or AllList is null

It's probably more likely that comlist is null. Could you show me where you construct comlist in your code?
Dilip_VDilip_V
Hi Alekya,

You wrote these lines 
wr.violation = violationMap.get(cl.ViolationId__c).Violation__c;
wr.guidance  = violationMap.get(cl.ViolationId__c).Guidance_to_Avoid_Violation__c ;
 wr.penalty   = violationMap.get(cl.ViolationId__c).Penalty__c;

But you are not checking for key in the map.So it would be better if you add IF condition before getting values from the map.
 
if(cl.ViolationId__c!=null&&violationMap.containskey(cl.ViolationId__c))
{
wr.violation = violationMap.get(cl.ViolationId__c).Violation__c;
               wr.guidance  = violationMap.get(cl.ViolationId__c).Guidance_to_Avoid_Violation__c ;
               wr.penalty   = violationMap.get(cl.ViolationId__c).Penalty__c;
}
Thanks.
Please let me know if it helps.
 
This was selected as the best answer
Alekhya Mathukumilli 7Alekhya Mathukumilli 7
Hi UC innovation 
this is where I constructed comlist
  list<Comliance_Checklist__c> comlist =[select id, Category__c,EventId__c,ViolationId__c,Notes__c,Topic_Discussed__c from Comliance_Checklist__c where EventId__c = : eventId ];
UC InnovationUC Innovation
Could you make sure you saved your class? It's odd that the null dereference error comes up on the } line. Looking at your code, I think Dilip may be correct. You probably need to do this:
 
if(!comlist.isEmpty()){
           for(Comliance_Checklist__c cl:comlist ){
             cidset.add(cl.Id);  
             if(cl.Category__c=='Outside/ Waste Area'){
               violationwrapper wr = new violationwrapper();
               //wr.violationid = cl.Id;
               wr.category  = cl.Category__c;
               wr.violationid = cl.ViolationId__c;  
			   
               if (violationMap.containsKey(cl.ViolationId__c))
			   {
					wr.violation = violationMap.get(cl.ViolationId__c).Violation__c;
					wr.guidance  = violationMap.get(cl.ViolationId__c).Guidance_to_Avoid_Violation__c ;
					wr.penalty   = violationMap.get(cl.ViolationId__c).Penalty__c;
			   }

               wr.notes     = cl.Notes__c;
               wr.topic     = cl.Topic_Discussed__c;
               dsnyList.add(wr);
               AllList.add(wr);
             }

The reason why it works on UAT but doesn't work on production is because the issue is data dependent. 
Alekhya Mathukumilli 7Alekhya Mathukumilli 7
Hello All,

The code worked.

Thank you for the support.
thank you Dilip.