You need to sign in to do that
Don't have an account?

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.
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
I wonder if you change this line:
to
And then use the Id in the .get()'s, Does that help? Best, Steve
Thanks for your reply, but it doesn't seem to have made a difference.
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.