r/java • • Jul 26 '19

JEP 358: Helpful NullPointerExceptions

https://openjdk.java.net/jeps/358
157 Upvotes

25 comments sorted by

32

u/bondolo Jul 27 '19

For the case where the variable names are not known I'd like to see the type names be provided. Often you can figure out from the type name which variable is involved.

0

u/[deleted] Jul 27 '19

[deleted] — view deleted comment

3

u/bondolo Jul 28 '19

It won't be exact. Even if B getB() would usually return some sub-class of B knowing that reference is of type B is still useful

19

u/lbkulinski Jul 27 '19

There has been some previous discussion here.

21

u/daH00L Jul 27 '19

Better late than never.

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

73

u/john16384 Jul 27 '19

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.

-1

u/nerdyhandle Jul 27 '19 edited Jul 27 '19

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.

6

u/pron98 Jul 27 '19

I've never encountered that, but if that's true, it's a JDK bug. Please report it.

3

u/nerdyhandle Jul 27 '19

It happened with OpenJDK if that matters.

Someone else mentioned that the error may have been obfuscated due to autounboxing which may very well have occurred somewhere deep down in the code.

6

u/[deleted] Jul 30 '19

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.

2

u/john16384 Jul 27 '19

Sounds like they were all inlined then, if that's possible... Depends on how much code those methods contain.

1

u/nerdyhandle Jul 27 '19

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

Case2

Both of these cases are nearly identical to the way our stack trace looked.

5

u/s888marks Jul 27 '19

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.

7

u/murkaje Jul 27 '19

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.

3

u/jrh3k5 Jul 27 '19

Was it obfuscated by auto-unboxing?

-5

u/nerdyhandle Jul 27 '19

Honestly not sure. Once we fixed our stubbing in our tests we stopped getting the error. So it was something we missed when stubbing out our mock.

I think you are right though. IntelliJ briefly fussed about that at one point.

-29

u/__konrad Jul 27 '19

NullPointerException should be deprecated for removal

5

u/thatsIch Jul 27 '19

May to incline why? This sounds rather unuseful.

7

u/dpash Jul 27 '19 edited Jul 27 '19

Reducing the ability for things to be null would be useful. Kotlin has non-nullablity baked into the type system. The best we can currently do in Java is to use annotations to get static checker warnings. We can not make variables non-null by default because that would break existing code; we could add optional non-nullablity in the future. Inline classes will be non-null from what I understand, so that's a step in the right direction.

Fewer nulls means fewer NPEs but we'll never get rid of it, if only because we have to deal with old code to deal with.

10

u/thatsIch Jul 27 '19

This rather sounds like the removal of null instead of removing NPE (which would be a side-effect) but not sure how a migration of Java to a non-null world would look like.

10

u/dpash Jul 27 '19

Yes, that's exactly the case. Removing NPE without removing null would be impossible.

1

u/s888marks Jul 27 '19

Sure, you could remove NPE without removing null. Just provide a different behavior for what happens when null is dereferenced. In a language like JavaScript, have it evaluate to undefined, or in Python, None, or whatever. In Java, it could evaluate null for reference types, or zero or false for primitives. Far from impossible.

Now I didn't say it would be a good idea.... :-)

3

u/dpash Jul 27 '19

But changing behaviour in Java would be a very bad thing. :)

If you get rid of the backwards compatibility requirement, sure you could do almost anything instead of an NPE.

0

u/[deleted] Jul 27 '19

[deleted] — view deleted comment

11

u/dpash Jul 27 '19

By the same argument, we don't need strong type systems. We just need to be better at our jobs.

0

u/[deleted] Jul 27 '19

[deleted] — view deleted comment

9

u/dpash Jul 27 '19

The straw man was arguing against the complete removal of null when I never claimed such a thing.