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.