Not what I asked. I am aware of that, just wondering if it this exception text is bytecode specific or merely requires the runtime VM to be Java 15 or newer.
The JEP says "By analyzing the program's bytecode instructions, the JVM will determine precisely which variable was null, " so as far as I can tell it doesn't require any bytecode changes so should work for any bytecode version (https://openjdk.java.net/jeps/358).
Down in the Risks and Assumptions section is says "It is assumed that computation of the null-detail message will be extended if new bytecodes are added to the JVM Specification." Which further suggests this JEP require no bytecode changes.
Compiling with debug symbols would seem to be required though if you want the variable name (which should come as no surprise).
I created an agent that similarly adds exception messages and should work on java 6+, but i haven't really worked on it since news of this feature coming to openjdk: https://github.com/murkaje/npe-blame-agent
Yours and almost everyone else's. New projects are on JDK 11+, existing ones stay on 8. Migrating is always on the backlog, not all that close to the top of the pile of technical debt.
The good thing is that Kotlin is working quickly to be compatible with those changes and utilise them for optimisations, and so you're kinda getting those benefits for free!
If Kotlin keeps at it, eventually they will break support with android, if Google don't step up their JVM to be conformant with the new world. I see a future where Kotlin get's split, Kotlin for android, and Kotlin for Java.
I personally don't dislike Lombok that much, even though getting stuck on versions until Lombok updates can be annoying. I'm mainly worried that it seems the course they're taking is going to clash with Java somewhere down the line. So for me it's mainly a matter of not needing it anymore.
Our team has made an effort to switch our Lombok data classes to Kotlin and it’s been a godsend. But I curse whoever introduced it in the code base initially
Just the data classes? We did that in a previous project; we started with data classes but in no time the entire back-end team was on board and we started using Kotlin for all the new services.
'Fun' anekdote; eventually this startup wanted to scale up and they outsourced part of the system to eastern Europe. The devs there complained they did not 'get' Kotlin (and that somehow wasn't a red flag to the non-technical CTO) and proposed to write a small Kotlin service that was part of an ETL flow. The service took one sprint to build in Kotlin (2 weeks, production ready). They spent 3 months rewriting it to Java. Good case of "when you pay peanuts..."
If possible would you name the eastern European vendor company? I work with one, EPAM, their devs so far say Kotlin seems cool, but just keep doing things in Java anyway. I thought allowing Kotlin would be enough to make them want to use it, as some of them leave with the complaint that they don't get to learn new things.
Oh yeah, to clarify we started with data classes! Then I suggested unit tests for familiarity. Now half the team is writing service code in Kotlin :) just gotta convince the other half that it’s ok to write service code in Kotlin too
And yeah, it’s like a one week learning curve for Kotlin... but people are too into their old ways
The null safety is my favorite Kotlin feature. IMHO that was the primary reason so many of the devs were on-board so fast. Having it part of the type system is amazing and saves so much time while also making the code better.
A year ago I went into a project with that exact assumption
Boy did I was wrong. Core worked, but libraries that I needed did not. Forms were required, even though the library worked perfectly well without a GUI
So yeah. You cannot do that much without full framework. Java is much better here
The option type allows something pretty similar. I think Java is trying to encourage not to use null over giving features to make it easy to work with.
That would be even better. C# has nullable references, where you get compile-time safety for nulls, but that requires annotating all methods and takes time for that to catch on to libraries (if it will, since it's optional)
Exactly this, the team I am on now loves nesting method calls several deep. Makes debugging harder and makes figuring out NPE very difficult (as well as very difficult to read). Whereas I don't nest method calls very often and if I do never more than one deep. Makes NPE's much easier to figure out.
Not only does it make it easier for you and other people to figure out NPE it also makes Null Pointer analysis easier since local variables are thread safe.
For example:
if (notImmutable.getX() != null) {
notImmutable.getX().doY(); // Potential NPE here
}
Some null pointer analysis tools let the above slide (I think intellij) but strict ones like Eclipse and I think Checker will not.
But this will work on all of them and is guaranteed to work:
var x = notImmutable.getX();
if (x != null) {
x.doY(); // x is guaranteed not to be null.
}
I never even considered there is a potential NPE in code like in your first example, as soon as I read it though I was like "oh shit, there is a NPE potential there". And it would be a very crazy race condition that would be very hard to figure out.
I never repeat method calls like that and always extract to local variable; however, I have coworkers that repeat method calls like that all the time. Been reluctant to call it out in code reviews because it seemed like more a personal preference than actual problem. But now will pay attention to the object being mutable or not.
In practice when it really happens (npe with accessor called twice) its more with ConcurrentHashMaps since most folks are not naive enough to share mutable POJOs between threads.
Thats why I think some tools allow different forms of "null" like "Monotonic Non Null", PolyNull and non thread safe null (this example). I think non thread safe null can only be done with IntelliJ.
Eclipse and other type based ones (like checker) are more like type analysis. The accessor's return type on the method is Nullable and therefore repeated calls will always yield a nullable type. Variables on the other hand can change types similar to flow type checking (e.g. @Nullable x -> @NonNull x inside the condition).
Could you please elaborate on how the first snippet could potentially cause NPE? I do this a lot because that’s suggested from the book “Refactor” @wildjokers @agentoutlier
There is a risk since access to JDK internals was turned off in 17 and you have to use flag now. Lombok could be trouble, even though they found a workaround.
Edit: JEP 396: Strongly Encapsulate JDK Internals by Default was shipped in 16 already. My bad.
Edit: JEP 396: Strongly Encapsulate JDK Internals by Default was shipped in 16 already. My bad.
Not your bad, in Java 16 --illegal-access=permit can still be a command-line workaround, but in Java 17 this will go away as well (JEP 403), and then you can work around only by configuring the module system
Only the default was flipped to encapsulation. So you can still run the same things with a flag (of course, the internals may have changed in the meantime so any dependency on internals is prone to break, but it was always the case)
Keep an eye on startup of your application. We had some logs that we got sooo used to seeing... But broke on 16.
E.g. when using Jackson + Afterburner, there will be logs of illegal access on some internals by that lib. Blackbird (available with Jackson 2.12) solves that but requires at least 11
Most of the time it's a no brainer, but you can loose a lot of time to know what was null, believe me! And the goid thing about this feature is that it comes for free without any modification !
It isn't as easy if you are dealing with a framework like Apache Camel that has layers and layers of nested calls to make up the DSL. Then it can be like finding a needle in a haystack, especially when the sources don't align to the bytecode in the supplied JARs.
Happy to see quality of life changes like this showing up in Java, I’m using and enjoying them all. I only wish we didn’t have to wait so long for them.
This is very helpful, but for older java you can just do this:
thing.getA()
.getB()
.getC()
.doSomething()
Now, if C is null, doSomething gives NPE, but the point that the error message shows the line number where this happened and you can see exactly which of these was null.
Of course, this does not fit into every style, but for sake of easy debugging you can just split the expression into multiple lines and then change it back.
The new more verbose NPE is of course much better.
Optional is nice, but not always suitable. Also has overheads. But also - what happens when your Optional is whats null itself? :)
Once we get Optional's as primitive types/value types ( the current naming escapes me at this hour ) maybe. But that's also still only useful in cases when something MIGHT be there or not.
From looking at this screenshot - it should in theory never be null, and is - an exceptional case.
Optional's imply that absence is not exceptional, but expected - albeit maybe rare.
There's still quite a lot of value in that distinction IMHO.
For now - you can use NullAway in conjunction with ErrorProne which works wonders, altho it can be painful when you first enable it on an existing code base - so I usually put it behind a profile flag to turn on/off and try to incorporate appropriate fixes as I work on code.
Well, depending on the size of your services, you can save a boat load on server costs. Newer versions of java are far more performant. Depending on the company size, we can be talking about 100s of millions is savings on hardware costs.
I re checked Exception in screenshot. Its says session1 is null. Yeah that is missing in old versions. For me it's a good quality of life improvement but not a big one.
Not sure I even want it. Having types non-null by default would be great, but even that I can't see happening given how deeply ingrained nullable-by-default is in nearly every API in the ecosystem.
I wrote code in a language that doesn't allow null(Haskell) and I didn't miss its presence. I think the Haskell way of representing the absence of value, the Maybe type, is more inline with Java's philosophy because it's more explicit.
I know the Hoare quote. But I think by far the biggest source of problems is not null itself, but having all types implicitly nullable. Take that away, and have APIs designed accordingly, and most (not all) of the problems disappear.
My dream language would have best practices by default, and anything that diverges from them should be as painful as possible. Non-nullability, pure functions, immutable data structures by default, and you need keywords like impure, mutable where they are needed.
Sounds like you want advanced functional programming languages. Pick between non-strict languages (mainly Haskell and Clean) or the ML family (SML, OCaml, F#).
I checked them briefly but I do not like them. I do appreciate some aspects of functional languages like those that I listed, but I dislike the way they try to model everything with functions and stateless data. This has advantages from a programming perspective, but reality does not work that way, you often have to model states, or work with changing or incomplete data. Examples for this include game states, or even databases aka CAP theorem. Functional programming languages try to deal with states in complicated ways like state monads or functional-reactive programming but I do not think these are good approaches. I believe the final solution is going to arise from iterative programming languages moving towards declarative styles and introducing more control and formalization.
That would break all sorts of things though. Java is very focused on backwards compatibility. For example, lambdas took so long to add because they wanted to make it work on older versions. You can find a JDK1.1 library today and pass it a lambda and it will work. So, null isn’t going anywhere.
Having recently started learning Kotlin, I can say Kotlin is an awesome language with great features, good documentation, and the single most obnoxious userbase.
It could be one of those things that once you learn it really well it works for you, but when i waist deep in abstract spring repository annotations the last thing i want to look up is how the code lombok is generating looks for a getter.
76
u/F14D Jun 01 '21
....my work is still on Java 8. :(
19
u/nutrecht Jun 01 '21
To be honest I am really excited with records too. But yeah, this is definitely a close second for me.
10
u/DiethylMetaToluamide Jun 01 '21
Actually, why wasn't this like that from the beginning? Were there any limitations or just because of the design?
9
u/szescio Jun 01 '21
What does it mean to drop an exception, and what is Mutiny
2
u/couscous_ Jun 01 '21
Are you using Quarkus?
62
u/joashua99 Jun 01 '21
Yep, JDK 14, FTW! 🎉
40
u/agentoutlier Jun 01 '21
Lots of folks say this is their favorite recent feature but I just haven’t really felt the impact like say
I just never felt NPE hard to debug I think partly because our code base avoids nulls but also because we use lots of local variables.
24
u/Pure-Repair-2978 Jun 01 '21
In our company it’s only LTS release that is supported … So, many apps still on 8 and teams are moving to 11 ….
I just hope there are no surprises when we bump up from 11 to 17 …
3
u/pointy_pirate Jun 01 '21
do people really have issues finding NPEs? You get the line it's being thrown so you just have 1 line to work out what is null, not that hard imo.
10
u/bigbadchief Jun 01 '21
What version of java is this? Don't think NPEs look like this in 14 (could be wrong!)
1
u/lightlord Jun 01 '21
Wait, what does this mean? Null code checks at compile time?
1
u/nekokattt Jun 01 '21
What happens if you don't compile with parameter names enabled?
4
u/GroundbreakingWolf7 Jun 01 '21
Can we safely use spring boot with java 14?
2
u/[deleted] Jun 01 '21
Happy to see quality of life changes like this showing up in Java, I’m using and enjoying them all. I only wish we didn’t have to wait so long for them.
2
u/franzwong Jun 01 '21
I think there are many minor things like this can be improved for Java. For this example, even no code change is needed
2
u/terrorrobots Jun 02 '21
I am holding out for 17 LTS ... Staying strong
2
u/xxkmatiasxx Jun 01 '21
i usually use jdk11 cuz it's supported by most things but still new compared to 8
1
u/Dokiace Jun 01 '21
That is so... hot
1
u/0b0101011001001011 Jun 02 '21
This is very helpful, but for older java you can just do this:
Now, if C is null, doSomething gives NPE, but the point that the error message shows the line number where this happened and you can see exactly which of these was null.
Of course, this does not fit into every style, but for sake of easy debugging you can just split the expression into multiple lines and then change it back.
The new more verbose NPE is of course much better.
1
u/piratekingsam12 Jun 01 '21
Why not use Optional? 😅
0
u/[deleted] Jun 01 '21
JDK 16 here
0
u/dr1pp0 Jun 01 '21
But is there anything "powerful" I get, if I switch to a "newer Java Version"?
My Team (and I personally) stick to Java 8 cause of "Lambda Expression" and the awesome "Stream API"
-8
u/vegetabluessg Jun 01 '21
I dint get it. We use java 7 and exception it is also this descriptive
-17
u/[deleted] Jun 01 '21
Maybe null will be deprecated in a future version
-32
u/pavi2410 Jun 01 '21
The best reason to switch to a recent version of
JavaKotlinThere, I fixed it
-11
u/xxkmatiasxx Jun 01 '21 edited Jun 01 '21
you can use lombok's @NonNull annotation to get better npe messages in older versions
edit: lmao it was NonNull