7

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.

  • 1
    I get "FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object AnonymousBlock: line 4, column 1" for your second example. – techtrekker Nov 4 '12 at 23:40
  • @techtrekker, Interesting, thanks. So that works as expected. May I ask what server you are on and API version was used? Did the first example break for you or work? – Daniel Ballinger Nov 4 '12 at 23:43
  • na8. Dev Edition. Executed using Exec Anonymous. – techtrekker Nov 4 '12 at 23:46
  • I just spotted this behaviour in a production on Summer '13. – Robert Sösemann Jul 9 '13 at 12:24
3

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.

  • Great, thanks. Could you provide me the case number (if possible). I'll reference it from my case. – Daniel Ballinger Nov 4 '12 at 23:46
  • 1
    Sure no problem, it is 08265588. – Andrew Fawcett Nov 4 '12 at 23:49
  • I just tested our repo from Dev Console on NA11 it reported line 11 as expected. So perhaps a fix has started to be rolled out? Or are you on an old API version in your class? – Andrew Fawcett Nov 4 '12 at 23:51
  • I was using API v24. From my tests with workbench I get the same results on na5 all the way up to v26. It appears the developer console is behaving differently to the other apis. – Daniel Ballinger Nov 5 '12 at 19:24
  • Yes I spoke to the developer that raised the case here and your correct developer console is not the way to reproduce this. – Andrew Fawcett Nov 6 '12 at 9:30
3

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...

  • Interesting, I tried using the web workbench to vary the API version from 21 all the way up to 26 on the na5 server. In all cases the workbench result gave me the incorrect line number. Then, as you found, if I run the same code in the developer console I get the correct line numbers. Very odd and a little concerning. What is the developer console doing that makes if different? I might go with if(((nullReferenceBoolean == true) == true) == true) to be extra safe :) – Daniel Ballinger Nov 5 '12 at 19:21
  • 1
    coming up next on thedailywtf.com: thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx 2.0 – eyescream Nov 5 '12 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.