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
Daniel BallingerDaniel Ballinger 

Incorrect line reference from NullPointerException

Can someone please verify that the following code is returning an incorrect line reference in the NullPointerException.

 

boolean nullReferenceBoolean = null;
List<long> listOfLongs = new List<long>();
for(integer i = 0; listOfLongs != null && i < listOfLongs.size(); i++) {
	System.debug(LoggingLevel.Debug, 'i:' + i + ' testLong:' + listOfLongs[i] + ' listOfLongs.size():' + listOfLongs.size());
}
if(nullReferenceBoolean) {
	System.assert(false, 'Expected Null Pointer Exception');
}
System.assert(false, 'Expected Null Pointer Exception');

I get the fatal error:

18:48:47.040 (40006000)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object
AnonymousBlock: line 3, column 1

I'd expect the error to be on line 6 and not 3.

 

The incorrect line number caused me to waste some time trying to debug the for loop rather than the next statement after it. The above is a greatly simplified version of the code I was working on so it wasn't immediately obvious what the issue was.

Best Answer chosen by Admin (Salesforce Developers) 
Daniel BallingerDaniel Ballinger

This appears to be resolved now. Running exactly the same code as before now gives a line reference of 6 as expected.

 

Oddly, I didn't really get any feedback from the support case other than that it now gives the expected result. So I have no idea why it works now and didn't a couple of days ago. Perhaps a release was made to the servers that addressed the issue...

All Answers

sanjdevsanjdev

Hi ,

 

Boolean variabale take the value of either True of false and not null. Please update "boolean nullReferenceBoolean = null;" to either true or false based on your requirement .

 

Mark it as resolved if it helps you.

Sanj.

Daniel BallingerDaniel Ballinger

Hi Sanj.

 

I understand why I'm getting the NullPointerException in the example and have deliberately caused it. The issue is that the line number referenced in the actual exception doesn't correlate to where the problem occured but rather the proceeding for loop.

 

Thanks,

Daniel

tomcollinstomcollins

This is an interesting but that you've found.  I was able to confirm it in the console myself.

 

Note that if you add a System.debug() immediately before where you expect the null pointer exception, the log will claim that it happened on that line.

 

For some reason, the log is showing the last line number successfully executed instead of the line with the error.

 

I'd say you've got a good example of the problem and you should open a ticket/case to have Salesforce look into it.

Daniel BallingerDaniel Ballinger

Thanks,

 

I've raised Case Number: 07939141 with support so I'll wait and see what they come back with.

Daniel BallingerDaniel Ballinger

This appears to be resolved now. Running exactly the same code as before now gives a line reference of 6 as expected.

 

Oddly, I didn't really get any feedback from the support case other than that it now gives the expected result. So I have no idea why it works now and didn't a couple of days ago. Perhaps a release was made to the servers that addressed the issue...

This was selected as the best answer