New in JDK 14: Better NullPointerExceptions

NullPointerExceptions are the ones that give 99% of Java programmers headaches. NullPointerExceptions is NPE. NullPointerExceptions are one of the most common exceptions in Java programs.

How do we handle NullPointerExceptions?

The usual thing is to look at the log and see what line went wrong. If this line of code is simple, it’s easy to find the problem.

The problem is that if the industry is complicated, it’s not so easy to figure out the problem. It is quite possible that we need to debug100 lines forward and 50 lines backward to solve the problem.

The biggest problem is that if this exception occurs in an online environment, debugging is not possible. It’s up to your eyes, your procedural acumen and your expertise to sort the problem out.

For example, let’s define CustUser and Address:

@Data
public class CustUser {
    private String userName;
    private Address address;
}
Copy the code
@Data
public class Address {
    private String addressName;
}
Copy the code

To generate an NPE:

@Slf4j
public class NPEUsage {

    public static void main(String[] args) {
        Address address=new Address();
        CustUser custUser=newCustUser(); custUser.setAddress(address); log.info(custUser.getAddress().getAddressName().toLowerCase()); }}Copy the code

The last line in the code above, because addressName is empty, throws an NPE when toLowerCase is called. The running results are as follows:

Exception in thread "main" java.lang.NullPointerException
	at com.flydean.nullpointerexceptions.NPEUsage.main(NPEUsage.java:16)
Copy the code

The above exception only tells us that there is an NPE on line 16. But there’s a long line of code on line 16, so where exactly is this exception being reported?

Simple code, such as the example we mentioned above, shows the problem with a simple analysis. But with such a cobweb of complex projects, it’s hard to find them.

Fear not, JEP 358: Helpful NullPointerExceptions is meant to solve this problem.

With the same recipe and flavor as above, we just need to add the following parameters at runtime:

-XX:+ShowCodeDetailsInExceptionMessages
Copy the code

Run it:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.toLowerCase()" because the return value of "com.flydean.nullpointerexceptions.Address.getAddressName()" is null
	at com.flydean.nullpointerexceptions.NPEUsage.main(NPEUsage.java:16)
Copy the code

See the difference? The complete error message is printed. The problem you’ve been thinking about has been solved.

This feature is great, but it is turned off by default.

There are pros and cons. Let’s see what effect this parameter has:

  1. Performance impact: There is performance pressure on stack Trace because of the extra information to store.

  2. Security implications: From the above example, we can see that the exception message contains a very substantial amount of code information. For some confidential applications, it is possible to infer the code logic from the exception information. This affects security.

  3. Compatibility: Finally, compatibility, previous JVMS did not store this additional NPE information, so there could be compatibility issues.

The article example github.com/ddean2009/learn-java-base-9-to-20

Welcome to pay attention to my public number: procedures those things, more wonderful waiting for you! For more, visit www.flydean.com