How to handle pre-Air Defense pointer exceptions in java code and java

Source: Internet
Author: User

How to handle pre-Air Defense pointer exceptions in java code and java

The NullPointerException encountered in the project is divided into two situations:

1. Reference an empty object, that is, call the method of the empty object or reference the attribute of the null object.

2. Assign the encapsulation class of the basic type in 8 to the corresponding basic class.

 

1. we need to make a non-empty judgment on the returned objects of other interfaces, because we do not know whether the obtained objects will be empty. For Collection Map, I usually call CollectionUtils MapUtils. For the returned String objects, I will call StringUtils. isNotEmpty. Among them, isNotEmpty not only judges NULL but also determines empty sets and empty strings. For example, query results from data. Pricing URL returned by the Workflow

2. for self-created objects, pay attention to what operations are performed on the object, and whether the object is null in the middle. If it is possible to add a non-null judgment, especially for set operations, it is easy to report a null pointer !!! So I always pay attention to the set of operations.

3. pay attention to the foreground domain objects, because these objects are created by the Framework. If I do not enter a value in the foreground text box, even though the background obtains an empty string when submitting the object, however, the probability of NullPointerException is very high.

4. Try to use the apache StringUtils class for String operations. This is very safe compared with String operations. For collection operations, the execution efficiency of apache CollectionUtils and MapUtils is also very high compared with that of the apache tool class, such as StringUtils. split ();

Some people have said that many judgments will affect performance. I personally think the performance sacrifice here is insignificant compared with the system security.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.