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 about Java running Times java.lang.NullPointerException?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to run the Java Times java.lang.NullPointerException, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to know about it.

Introduction

Java exception, I believe all programmers are familiar with. But it may not be so clear about how to classify it, how JVM handles it, how it is handled in the code, how it should be used, how it is implemented at the bottom, and so on. I found a structure diagram of the Java exception here, as shown in the following figure:

Note: the following is a detailed introduction to null pointers.

First, a null pointer throws an exception, as shown in the following figure:

Second, the cause of the exception

1. String variable is not initialized

2. Objects of interface type are not initialized with specific classes, such as:

Map map / / will report an error

Map map = new Map (); / / will not make a mistake.

3. When the value of an object is empty, you do not judge it to be empty.

4. Compare a string with a text. The text can be a string or an element of Enum. An exception will occur as follows

String str = null

If (str.equals ("Test")) {

/ / the code here will not be triggered because a java.lang.NullPointerException exception will be thrown.

}

5. Use String.valueOf () method instead of toString () first.

Avoid using the toString method of an object when your program code requires a string representation of the object. If the reference to your object is equal to null,NullPointerException, it will be thrown, using the static String.valueOf method, which will not throw any exceptions and print "null"

6. Class is declared. By default, class = null;. When calling a method in class, the system can only give you a null pointer exception. Just instantiate it: class = new Class ()

7. Null is returned, and the return value of the method is not defined as a general type, but an array. In this way, you can avoid a lot of unnecessary NullPointerException if you want to return null.

Thank you for reading this article carefully. I hope the article "what to do about Java running Times java.lang.NullPointerException" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Development

Wechat

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

12
Report