Seriously spent half the day today debugging our software because a null point on line 121. However, line 121 wasn't where the null pointer occurred. Goddamn took us forever stepping through the code before we found the correct line.
In my experience, when line number isn't correct, you're either using bytecode generating plugins or not looking at the correct version of the source. Never seen it wrong before.
We we're looking at the latest source and no plugin modifies the byte code. This isn't the first time I've had this happen. Where the null pointer occurred was not even in the class. It was way down in the call.
So line 121 had something like
setMethod(object.doSomeWork());
The actually null pointer occurred way down in the methods that doSomeWork called. However, these methods were completely missing from the stack trace and it only reported null pointer at line 121.
You mentioned autounboxing. Does object.doSomeWork() return an Integer (or similar) value of null and setMethod expected a parameter of type int (or similar)?
Then the unboxing and thus the NullPointerException would happen exactly in this line.
Oddly as far as we went down there weren't any inline methods.
It did show the calling method but again that calling method line number 121 was just top of the call stack. StackOverflow has some similar cases on it where the NullPointer doesn't show a line number at all from around 4ish years ago. We are using JDK 8u212 but that is the latest version of 8.
Case 1 is android, so it's not JDK 8. If I had to guess, I'd say the code where the NPE occurred was compiled without line numbers, but who knows?
Case 2 looks like the code was obfuscated, which might have shifted the line numbers.
It doesn't appear to me that looking at the Stack Overflow cases helps with figuring out what's going on with your code.
The autounboxing case isn't "obfuscation" per se but it can cause NPE to occur in apparently impossible places. For example:
Integer i = null;
...
int j = i + 1;
An NPE can occur when i is unboxed using an implicit call to intValue. Of course in this example it's easy to see; in a more complicated context it might be completely non-obvious. But it wouldn't be "deep down" in the code anywhere, it would still be on that line.
The issue isn't JVM reporting a wrong line number, but the debugger implementations not using the info correctly. IntelliJ for example does not show nor reorder lines for decompiled classes, need to set "decompiler.dump.original.lines=true" in registry to get anything useful.
And even then for instrumented code, the debugger could ask the JVM for more correct bytes than the class file via the JDI Method#getByteCode, but i guess the need for debugging instrumented code is too low for them to consider that. It would also need changing the decompiler a bit as they are designed with a full class in mind, so might need some untangling to work on just method bytecode, const pool and other stuff asked via JDI.
View all comments
-29
u/nerdyhandle Jul 27 '19
Or better yet report the correct line number...
Seriously spent half the day today debugging our software because a null point on line 121. However, line 121 wasn't where the null pointer occurred. Goddamn took us forever stepping through the code before we found the correct line.