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
admintrmpadmintrmp 

Attempt to de-reference an (not so) null object

...

if ( allOtherBookings.containsKey(objId) && objId != null && allOtherBookings.get(objId) != null ) {
	for ( otherbooking__c o : allOtherBookings.get(objId) ) {

...

 

Salesforce likes to magically give me errors on objects which simply cannot be null! The error points to the for loop.

Alex.AcostaAlex.Acosta

are you sure your map is not null and has been intialized?  Best advice I can give without know the context of the code is system.debug each line so you know where the nullpointer is coming from.

admintrmpadmintrmp

Hi Alex,

 

This post was a bit of rush.

 

I failed to mention that I have done several system debugs and the map is not returning null and its returning values fine. I've done the usual procedures of debugging but I fail to see why Salesforce is giving me an error.

 

Salesforce also gave me an error when I commented out the entire block of code, and it returned the same error but for the line above. So I commented that out, and got the error for the line above that (these are all operating on distinct variables that have all been clearly defined and populated in the method).

 

At one point I resorted to commenting out the entire method, finding that the error had gone, placed back the method code, and the error had gone magically. A few moments later, the error was back to haunt me again.

 

I'm going to spend today having another look at it. It may be something to do with a declaration at class-level, but I'm still highly doubting that.

admintrmpadmintrmp

Although I wish not to post the entire method here (it's rather large and complex), I would like to mention where I think the problem might be in pseudo.

 

Essentially, what is happening here is that a record has a start date and an end date. I am splitting the record up into multiple days by cloning the record, adding them to a list and then setting them to a map (<Id, sObject[]>).

 

I have tested the clone with the id preserved and not preserved.

 

When it comes to the loop (in my first post), the issue arises but the map is NOT null. The only explanation here is that the object being returned from the loop is somehow corrupt in a way. I have done a system debug on the records, but there are no null fields.

 

I've also commented the contents of the loop, and there are no issues there.

Alex.AcostaAlex.Acosta

Only real thing I can see if while you're looking through your results you might have either a null object within your collection or a collection that was never declared but do have an existing key value. At least this is the only result I can see. Sorry I can't help much with this case as you mentioned it is in part due the large and complex code you are writing. Did you ever get to figure out if this was happening at the IF condition or at the loop portion of your code?

admintrmpadmintrmp

Hi Alex,

 

Unfortunately I couldn't resolve this. I had tested the method down to the ground and couldn't find any issues. I feel as if there was a problem with the compiler understanding what was needed.

 

What I have done instead is refactored the entire process of what I was trying to achieve and it's now working as expected. The code is not very similar, so I came across a lot less issues that were much easier to fix.

 

Thanks for your suggestions anyway Alex.