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
DownstairsBDownstairsB 

Map.get() method always returns NULL on developer edition

Hi, I'm having a similar issue with using a Map<id,Product2>

 

The weird thing is that it works perfectly in sandbox but not in development... wtf apex?

 

I'm not going to post the entire trigger code here, as it's not necessary, I have narrowed the issue down to the Map .get() method:

 

24 System.Debug('Keyset: '+pMap.keyset());
25 System.Debug('Product: '+pMap.get('01t50000001LcjyAAC').name);
26 Product2 testp;
27 if (Trigger.isInsert){
28 for (Product_Lot__c pl: Trigger.new){
29           System.Debug('Product__c: '+pl.Product__c);
30           testp = pMap.get(pl.Product__c); <--- Always Returns Null
31           testp.Inventory_on_Hand__c += pl.Quantity__c;    <--- Causes a null pointer exception
32      }

 

 

Results of Execute Anonymous:

 

11:33:25.101|USER_DEBUG|[24]|DEBUG|Keyset: {01t50000001LcjyAAC}

11:33:25.101|METHOD_EXIT|[24]|System.debug(ANY)

11:33:25.101|METHOD_ENTRY|[25]|System.debug(ANY)

11:33:25.101|METHOD_ENTRY|[25]|MAP.values()

11:33:25.101|METHOD_EXIT|[25]|MAP.values()

11:33:25.102|USER_DEBUG|[25]|DEBUG|Product: 2-Mercaptoethanol

11:33:25.102|METHOD_EXIT|[25]|System.debug(ANY)

11:33:25.102|METHOD_ENTRY|[29]|System.debug(ANY)

11:33:25.102|USER_DEBUG|[29]|DEBUG|Product__c: 01t50000001LcjyAAC

11:33:25.102|METHOD_EXIT|[29]|System.debug(ANY)

11:33:25.102|METHOD_ENTRY|[30]|MAP.get(ANY)

11:33:25.102|METHOD_EXIT|[30]|MAP.get(ANY)

11:33:25.102|EXCEPTION_THROWN|[31]|System.NullPointerException: Attempt to de-reference a null object

11:33:25.102|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Trigger.Product_Lot_Trigger: line 31, column 4

 

So if you look at the highlighted lines (DEBUG outputs), you can plainly see that the map contains a valid ID, and the

hard-coded get() on line 25 returns the correct object and displays the correct name, so the Value is correct too.

Also you can see that the field Product__c (on line 29) exactly matches the key that is in the Map.

But line 30 is still returning NULL!!!!

 

I'm just starting to get angry at this point, because this works 100% correctly in the Sandbox.  Can anyone help?

Best Answer chosen by Admin (Salesforce Developers) 
David81David81

Are you sure it's returning null? I think the problem is actually on line 31. Does the object you are getting from you map have a non-null value for "Inventory_on_Hand__c" and do all the Product_Lot__c objects in your loop have a non-null value for "Quantity__c"?

 

If any of these values are null you'll get the error you are seeing.

All Answers

Cory CowgillCory Cowgill

is pMap declared as static?

 

are you processing bulk records in Developer versus Sandbox?

 

 

Cory CowgillCory Cowgill
Checkout line 26. Your always declaring this guy as null before your if statement. Based on lines 26 to 32 this guy is always going to be null. Try removing line 26.
24 System.Debug('Keyset: '+pMap.keyset());
25 System.Debug('Product: '+pMap.get('01t50000001LcjyAAC').name);
26 Product2 testp;
27 if (Trigger.isInsert){
28 for (Product_Lot__c pl: Trigger.new){
29           System.Debug('Product__c: '+pl.Product__c);
30           testp = pMap.get(pl.Product__c); <--- Always Returns Null
31           testp.Inventory_on_Hand__c += pl.Quantity__c;    <--- Causes a null pointer exception
32      }
Shashikant SharmaShashikant Sharma

I am pretty sure that when you fill the map the value for this

 

pMap.get(pl.Product__c) is null

 

I would like to see how have you filled the Map. Please add a 


system.debug(' *************** pMap :  ' + pMap);

before your loop starts.

 

anda system.debut(' *********** pl.Product__c ' + pl.Product__c); just before you get null pointor exception.

David81David81

Are you sure it's returning null? I think the problem is actually on line 31. Does the object you are getting from you map have a non-null value for "Inventory_on_Hand__c" and do all the Product_Lot__c objects in your loop have a non-null value for "Quantity__c"?

 

If any of these values are null you'll get the error you are seeing.

This was selected as the best answer
DownstairsBDownstairsB

Hi David, your post helped me, you were right about Null values.

 

I had just assumed that if a number field is blank, it's treated as a 0.  When in fact it's treated as a NULL.

 

So the issue was with the Product2 that I was using to test it.  Specifically with the Inventory_on_Hand__c field.

It should have been 0 by default, but in fact it was blank (NULL)

 

So when I tried to add the Quantity__c, it throws a null pointer exception.

When I set Inventory_on_Hand__c to 0 then I didn't get the exception!

 

Thanks for your help everyone, sorry if I wasted your time!

 

David81David81

Downstairs,

 

I noticed when you posted your full trigger + test that you were hardcoding your Product Id in the test method even though you were creating a product record in the same method. Not sure if there was a particular reason for that, but it seemed odd to me.

DownstairsBDownstairsB

Yah i was kind of cutting the code up a bit, I had some other test stuff in there before, which is why it worked in the sandbox edition, but it was failing when I tried testing it on actual records in SF.

 

Thanks again!

 

ps I deleted the post with the full trigger, as it turned out not to be a problem with my code, after all :)

David81David81

No problem. Glad I could at least nudge you in the right direction :)

David Roberts 4David Roberts 4
I had a similar problem using a map.get within a loop.
Found that I had to seperate the get into a temporary list:

list<Custom_Object__c> templist = MyMap.get(lookupItem);
                if (templist != null){
                    
                    for(Custom_Object__c listToWorkOn:templist ){
                        //  do stuff to listToWorkOn
                    }
                }