Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the java null pointer exception?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what is the java null pointer exception". In the daily operation, I believe that many people have doubts about what the java null pointer exception is. The editor consulted all kinds of data and sorted out a simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "what is the java null pointer exception?" Next, please follow the editor to study!

1: what is a null pointer exception?

Call the instance method of the null object.

Access or modify the fields of the null object.

Take null as an array and get its length.

Access or modify the time slices of the null as an array.

Throws null as a Throwable value.

2: how did it come about?

1) the return type is a basic data type. When return wraps objects of data type, it is possible to unpack NPE automatically. Counterexample: public int f () {return Integer object}. If it is null, the NPE is automatically unboxed.

2) the query result of the database may be null.

3) even if the elements in the collection are isNotEmpty, the data elements taken out may be null.

4) when a remote call returns an object, null pointer judgment is required to prevent NPE.

5) for the data obtained in Session, it is recommended that NPE check to avoid null pointers.

6) cascading calls obj.getA (). GetB (). GetC (); a series of calls are easy to produce NPE. Positive example: use JDK8's Optional class to prevent NPE problems.

3: how to prevent

① object prevent, direct! = null

② collection class null: generally use! = null&& to judge size (), or call isEmpty () method, or use Collection tool class to determine null, java8 Optional class

③ string null: need to judge whether = = null&& "" .equals (str) to judge, or StringUtils tool class to judge

In addition, in the project, all foreground parameters, objects, database query statements, JSON objects, JSON arrays, and values after get () should be null.

4:Optional class

The Optional in JDK8 is actually a container: it can hold the value of type T, or just null. Optional provides many useful methods so that we don't have to explicitly detect null values.

Optional opt = Optional.empty (); Optional opt = Optional.of (new Object ()); Optional opt = Optional.ofNullable (null); if (opt.isPresent ()) {System.out.println (opt.get ());} else {System.out.println ("no data");} at this point, the study of "what is a java null pointer exception" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report