NullPointerException Causes and Solutions of Null Pointer Problems

While creating and running a springboot project, I accidentally encountered the following exception.
Null pointer exception
So I checked various blogs and sorted out the reasons and solutions.

Reasons for NullPointerException

There are only two situations:

  • Asked properties using null object method
  • Method accessed using null object

After finding the cause, we can start troubleshooting

NullPointerException solution

  • 1) Check whether the value of the object accessing the property or method in the line of code where the null pointer exception occurs is null;
  • 2) If the object accessing the property or method is null, check the assignment method of this object

If assigned by spring, the class in which the object is located needs to be managed by spring, and whether this attribute is described by annotations such as @Autowired or whether it is assigned through the constructor or set method)

  • 3) If it is a springboot project unit test class, check whether the @Test annotation on the unit test method is correctly introduced.

For example: whether it is org.junit.jupiter.api.Test

Run the program again and the program starts successfully~

Guess you like

Origin blog.csdn.net/weixin_45015214/article/details/111830048