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
Roth2842Roth2842 

Map containskey() generates de-reference a null object

I am created a batchable class, but when I am trying to run it, I am getting errors that I don't understand.

 

 

global class RefreshPipelineProcessor implements Database.Batchable<sObject>, Database.Stateful {
...
private Map<Id,User> activeUsers {
get {
...
global void execute(Database.BatchableContext ctx,List<Sobject> scope){
for (OpportunityFieldHistory ofh : lofh){ Opportunity o = ofh.Opportunity; User u = o.Owner__r; system.debug('Variable Settings:\n Opportunity: ' + o + '\nUser: ' + u + '\nUID: ' + u.id + '\nActiveUsers: ' + activeUsers.get(u.id)); if (activeUsers.containsKey(u.id)) { ...

(the System.debug was inserted to figure out what was going on.  I don't really call Map values with out testing for their presence)

 

The debug log:

 

 

11:27:36.221|USER_DEBUG|[86]|DEBUG|Variable Settings:
Opportunity: Opportunity:{Has_Quote__c=True, Owner__c=00580000003P8glAAC, Id=006Q00000046PZ4IAM}
User: User:{ManagerId=00580000002MQKSAA4, IsActive=true, Id=00580000003P8glAAC}
UID: 00580000003P8glAAC
ActiveUsers: User:{ManagerId=00580000002MQKSAA4, IsActive=true, Id=00580000003P8glAAC}
11:27:36.221|METHOD_EXIT|[86]|System.debug(ANY)
11:27:36.221|METHOD_ENTRY|[87]|MAP.containsKey(ANY)
11:27:36.221|EXCEPTION_THROWN|[87]|System.NullPointerException: Attempt to de-reference a null object
11:27:36.221|METHOD_EXIT|[87]|MAP.containsKey(ANY)

 

The activeUsers Map appears exists, not be null, and has the key in question, but I don't understand why calling .containsKey() against it is returning an error.

 

 

Any insight would be greatly appreciated.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Roth2842Roth2842

I believe the issue turned out to be scoping.  I was trying to set the property from within it.  Which I am willing to assume was creating a local variable, instead of setting the property, which in turn was released once the section exited.

 

The solution was to create a private member variable and set that.

All Answers

SteveBowerSteveBower

I wonder if you change this line:

 

User u = o.Owner__r;

to 

 

Id u = o.OwnerId;

 

And then use the Id in the .get()'s, Does that help?   Best, Steve

 

Roth2842Roth2842

Thanks for your reply, but it doesn't seem to have made a difference.

 

85 User u = o.Owner__r;
86 id uid = o.OwnerId;
87 system.debug('Variable Settings:\n Opportunity: ' + o + '\nUser: ' + u + '\nUID: ' + u.id + '\nActiveUsers: ' + activeUsers.get(uid));
88 if (activeUsers.containsKey(uid)) {
...


13:42:38.120|USER_DEBUG|[87]|DEBUG|Variable
Settings:
Opportunity: Opportunity:{Has_Quote__c=True,
Owner__c=00580000001tV1SAAU, OwnerId=00580000001tV1SAAU,
Id=006Q0000004zL6kIAE}
User: User:{ManagerId=00580000001tV1TAAU,
IsActive=true, Id=00580000001tV1SAAU}
UID: 00580000001tV1SAAU
ActiveUsers:
User:{ManagerId=00580000001tV1TAAU, IsActive=true,
Id=00580000001tV1SAAU}
13:42:38.120|METHOD_EXIT|[87]|System.debug(ANY)
13:42:38.120|METHOD_ENTRY|[88]|MAP.containsKey(ANY)
13:42:38.120|EXCEPTION_THROWN|[88]|System.NullPointerException:
Attempt to de-reference a null object
13:42:38.120|METHOD_EXIT|[88]|MAP.containsKey(ANY)

 

 

 

Roth2842Roth2842

I believe the issue turned out to be scoping.  I was trying to set the property from within it.  Which I am willing to assume was creating a local variable, instead of setting the property, which in turn was released once the section exited.

 

The solution was to create a private member variable and set that.

This was selected as the best answer