Java 8 gives us a better solution

Preface

In normal business development, null pointer is a problem we often encounter,

It may lead to the failure of our process or some unexpected situations.

This is the reason why we need to avoid null pointers. How can we solve this problem?

Null pointer scenario

1. Packaging type field, because there is a null pointer for automatic unpacking;

2. The A object contains the B object. When the B object field is obtained through the A object, the method in the B object is directly called without judgment, and a null pointer appears;

3. String comparison, null. equal ("string") has null pointer

4. The list returned from the remote is not an empty array but null. A null pointer appears when you operate on it.

How to troubleshoot the pointer problem above the line

In the daily development process, we usually check the log to find out the problem of null pointer. If the log is not in place, we can only trace the code through the location thrown by NullPointerException.

This requires us to print logs when writing code

1. Call the entry of the method to print the input parameter, and print the output parameter with the result returned by the method

If log printing is not done well in advance, we can consider using Alibaba's Java diagnostic tool Arthas to deal with it

1. After Arthas is started, the JVM process is obtained

2. Monitor the input of the method through the watch command

reflection

For this problem, I summarized some methods I used in my work,

The most direct operations are to eliminate the possibility of null pointers at the root, and perform null detection before operation.

Let's take the product information as an example. We need to get the name of his shop. How would you write it

1. Defensive check: every variable is checked for null. Every time you are uncertain whether a variable is null, you need to add a nested if block, which increases the number of code layers.

2. Fast failure check. Every null check is an exit point and returns a fixed string. However, it is unavoidable to forget to check a variable.

3. Manual control, strict control of data, no non empty fields, but it is difficult to ensure that all data is normal

4. Use the optional in Java 8 to control, model the missing value, and simply encapsulate the class when the variable exists. When the variable does not exist, the missing value will be modeled as an empty Optional object

1. Create an Optional Encapsulated ProductVO object

2. Convert Optional to Optional

3. Use map to convert Optical to Optional

4. Call any method on the chain and return a null, then the result is the default value we set

Among the above methods, we have seen the fourth method, which uses only one line of code to help us eliminate null pointers. However, Optional in Java 8 needs to use it in combination with multiple methods. Now there are many detailed methods on the Internet, so we won't introduce them too much.

Note the difference between flatMap and Map

In the operation of stream streams, flatMap and Map are the methods we often use. Many people do not understand the difference between them.

Map (): map applies a function to each element in the list and returns the list of applied elements.

FlatMap(): flatMap is a common combinator that combines mapping and flattening. FlatMap requires a function that handles nested lists and concatenates the results.

We can see that their results are very different

The Map method converts the list into a result set of three small List objects

The FlatMap method is based on the Map method. It does one more flattening operation, converts the results into a primary structure, and takes out all the results.

Is there a null pointer problem in this example? If so, how do you modify it?

summary

For the null pointer problem, it seems that it is not a big problem, but it affects the normal operation of online businesses, which is definitely not possible.

We must have a clear idea to solve this problem

1. In advance,

• Be sure to print the log to facilitate troubleshooting;

• When implementing business logic, if you are not sure about the object you are operating on, you must check the blank before operating;

• For null pointers of string type, we can use Objects for comparison;

• Input verification of required fields

2. In the process, since it has already happened, we need to quickly locate the problem through logs and Arthas tools, quickly repair and go online to reduce the time of failure:

3. After the event, we can strengthen code review to review our own code to avoid the recurrence of such situations.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us