New feature of JDK 14: nullpointerexceptions for easier use

New feature of JDK 14: nullpointerexceptions for easier use

The exception that makes 99% of Java programmers headache is nullpointerexceptions. Nullpointerexceptions is called NPE for short. It is a kind of runtime exceptions, and it is also the most prone exception in Java programs.

What do we do when nullpointerexceptions occur?

Generally, it is to look at the log to see which line is wrong. If there is only simple code in this line, it is easy to find the problem.

Unfortunately, if the business is complex, it's not so easy to find the problem. Most likely, we need to debug100 lines forward and debug50 lines backward to solve the problem.

The biggest problem is that if this exception occurs in the online environment, it is impossible to debug. At this time, it depends on your naked eye, your sensitivity to the program, and your professionalism to find out the problem from the flowers.

For example, we define a custuser and address:

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

Another NPE is generated:

@Slf4j
public class NPEUsage {

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

In the last line of the above code, because addressname is empty, NPE will be thrown when tolower case is called. The operation results are as follows:

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

The above exception only tells us that there is an NPE on line 16. But there is a long string of code in line 16. Where did this exception report?

Simple code, such as the example we mentioned above, can be simply analyzed to know the problem. But for such complex projects as cobwebs, it is difficult to find them.

Don't be afraid, JEP 358: helpful nullpointerexceptions is used to solve this problem.

For the above example and the above formula and taste, we only need to add the following parameters at runtime:

-XX:+ShowCodeDetailsInExceptionMessages

Run the following:

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)

See the difference? The complete error message was printed out. Your problem has been solved.

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

Advantages have disadvantages. Let's look at the impact of this parameter:

Examples of this article https://github.com/ddean2009/learn-java-base-9-to-20

Welcome to my official account: the procedures, the more wonderful things waiting for you! For more information, please visit www.flydean.com com

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>