Find your content:

Search form

You are here

Incorrect line reference from NullPointerException

 
Share

Firstly, this has happened to me in the past, I raised Salesforce support case 07939141, and it appeared to be resolved (with no notes on what was done to fix it other than that it now works).

Now it is happening again, and I'm not sure why. The simple answer would be to not cause null pointer exceptions. However, with more complicated code I've found this can make debugging issues more difficult as the line number doesn't correspond to where the exception occured.

When I run the following as anonymous apex...

boolean nullReferenceBoolean = null;
List<long> listOfLongs = new List<long>();
listOfLongs.add(5);

for(integer i = 0; listOfLongs != null && i < listOfLongs.size(); i++) {
    System.debug(LoggingLevel.Debug, 'i:' + i + ' testLong:' + listOfLongs[i] + ' listOfLongs.size():' + listOfLongs.size());
}

//This should cause the nullReferenceException on line 10
if(nullReferenceBoolean) {
    System.assert(false, 'Expected Null Pointer Exception');
}
System.assert(false, 'Expected Null Pointer Exception');

I get the fatal error (Note line 5 rather than line 10):

15:12:43.241 (241712000)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object AnonymousBlock: line 5, column 1

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

I've found another even more simplified example:

boolean nullReferenceBoolean = null;
boolean foo = true;
//This should cause the nullReferenceException on line 4
if(nullReferenceBoolean) {
    System.assert(false, 'Expected Null Pointer Exception');
}
System.assert(false, 'Expected Null Pointer Exception');

This gives (Note line 1 rather than line 4):

System.NullPointerException: Attempt to de-reference a null object AnonymousBlock: line 1, column 1

I'll chase this up with Salesforce support again, but does this break for anyone else? Perhaps it is an Org or Server specific issue? A link to a known issue would be ideal if possible.


Update from Salesforce support

Salesforce support got back to me confirming that this is indeed a known issue.

Frustratingly, they weren't able to provide a known issue reference. So the only way to get updates on it is to raise your own support case.

One interesting thing that did come out was setting the Apex Code logging level to Finest. With this adjustment the exception does produce the correct line number.


Attribution to: Daniel Ballinger

Possible Suggestion/Solution #1

We have hit this and raised a case 08265588 with Salesforce on the 16th October. The status of the case is 'Bug Fix Submitted', with the following comment.

As discussed, Tier 3 has provided an update wherein they have informed that this issue is a known issue which is already under our observation and our Product Development team is working on it. Unfortunately, I will not be able to provide you a date when this issue will cease to exist. For now, we will change the status of the case to "Bug Fix submitted" .

This was the repo we applied to the case...

Boolean b=true; 
if (b) 
{ 
    system.debug('b is true in block 1'); 
} 
else 
{ 
    system.debug('b is false in block 1'); 
} 
b=null; 
if (b) 
{ 
    system.debug('b is true in block 2'); 
} 
else 
{ 
    system.debug('b is true in block 2'); 
} 

I'll line number the snipped below for discussion

1 Boolean b=true; 
2 if (b) 
3 { 
4     system.debug('b is true in block 1'); 
5 } 
6 else 
7 { 
8     system.debug('b is false in block 1'); 
9 } 
10 b=null; 
11 if (b) 
12 { 
13    system.debug('b is true in block 2'); 
14 } 
15 else 
16 { 
17     system.debug('b is true in block 2'); 
18 } 

Line 11 should generate a NPE. (A null condition in an if statement causes an NPE). However, the compiler reports the error in line 4.

System.NullPointerException: Attempt to de-reference a null object AnonymousBlock: line 4, column 1

You can experiment with various themes, adding lines that execute and dont execute, and see that the delta seems to be related to the number of executed lines, not the actual number of code lines.


Attribution to: Andrew Fawcett

Possible Suggestion/Solution #2

Wicked :D

Reproduced without problems on a Dev Edition, Unlimited Ed and a sandbox from this Unlimited Ed. But - I was doing this from Real Force Explorer that uses API v 21.

When run from developer console I got expected behaviour (line 10 in 1st example, line 4 in 2nd). So maybe they really fixed something and you're calling old API?

You can also make your code look stupid but behave as expected - if(nullReferenceBoolean == true) will behave ok...


Attribution to: eyescream
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4029

My Block Status

My Block Content