[Solved] Mybatis error: attempted to return null from a method with a primitive return type (int)

1. Origin of the problem

When viewing the log, I found that a line of error message was printed in the log:

The data of assembled pets that have been released is abnormal — & gt; Mapper method ‘applets. user. mapper. xxxMapper. xxxmyRank attempted to return null from a method with a primitive return type (int).

The meaning is well understood. The query method of xxxmyrank in a mapper file returns null, but it needs to return an int type number, so an error is reported.

2. Problem analysis

Fdafdsaf query is a simple query used to query a ranking. If you query the ranking yourself, you can use int type to receive it. At the beginning of the test, there was no problem, but after the project was really launched

Report this problem before you know that this problem exists in the previously written code. The reason for this problem is that the database does not query the corresponding data, so it returns a null value. When you write your own code, you use the int type receive. As shown in the figure below:

3. Solution

After finding the cause of the problem, it is easy to solve it. Change the received int type to the wrapper type integer type class for reception, so as to solve this problem. Because the integer type can be null, regardless of the specific value returned

Or a null value, can receive. Then, you can process the null value in the code, and the problem is solved.

Expansion: this method is mainly used in query. When writing custom numeric fields in Java code, it is best to use the wrapper type to receive them,

Byte,Boolean,Short ,Character,Integer,Long,Float,Double.

The principle is the same. If you query the entity class or use the entity class to accept the value passed by the front end, there may be no value null. At this time, using the wrapper type can solve this problem.

Similar Posts: