Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to enhance NPE NullPointerException in Java14

Shulou Source: shulou.com Published: 2022-06-01 22:39:12 09月17日 Update

In view of how to enhance NPE NullPointerException in Java14, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Improve NullPointerExceptions to improve the availability of exceptions generated by JVM by describing exactly which variables are null. The authors of the proposal hope to provide developers and support staff with useful information about why the program terminates abnormally, and to improve understanding of the program by more clearly associating dynamic exceptions with static program code.

String name = user.getLocation (). GetCity (). GetName ()

Before Java 14, you might get the following error:

Exception in thread "main" java.lang.NullPointerExceptionat

NullPointerExample.main (NullPointerExample.java:2)

Unfortunately, if line 2 is an assignment statement that contains multiple method calls (such as getLocation () and getCity ()), any one of them may return null. In fact, the variable user may also be null. Therefore, it is impossible to determine who caused the NullPointerException.

In Java 14, the new JVM feature can display more detailed diagnostic information:

Exception in thread "main" java.lang.NullPointerException:

Cannot invoke "Location.getCity ()"

Because the return value of "User.getLocation ()" is null

At NullPointerExample.main (NullPointerExample.java:2)

The message contains two clear components:

Consequence: Location.getCity () cannot be called

Reason: the return value of User.getLocation () is null

This is the answer to the question about how to enhance NPE NullPointerException in Java14. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Tags: Programs questions people information variables methods more help answers easy clear easy unfortunately two code author content dynamics reasons usability Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Linux MySQL Shulou Technology MariaDB