(Solved) org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, “staffId”)

Problem phenomenon:

        I encountered a mybatis error in the project today:

        org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "staffId")

        This is a problem I have never encountered before, it is very interesting, so I will record it here.


problem analysis:

       According to the error log, at first I thought it said:

        When executing the sql of mybatis, the value of the parameter staffId is null, so an error is reported!

But after thinking about it, I found something wrong. If the staffId is null and the database is a non-empty field, its error should not be the current log, but should be:

    Column 'staff_id' cannot be null

        So I had to debug the project to see what caused the problem.

        Finally, the dao operation that caused the problem was located:

         Through debugging, it turns out that the incoming parameter xxxEntity is null, and the basic dao operation methods such as updateById provided by mybatis do not support passing parameters as null, which leads to the error at the beginning of the article!

        In fact, if you can see or understand the error message clearly, it is not difficult to know the problem:

    source is null

         It means that the resource object (parameter xxxEntity) is null, so when getting the staffId of the null object, it is also null.

        Now that you know what the problem is, isn't it easy to solve the problem!


expand:

        Question: The resource object (parameter xxxEntity) is null, so why is the error message assigned to the staffId parameter?

        Answer: In fact, this has something to do with the serialization of resource objects , because staffId is the first field of this entity object, so when using the get method to obtain the value of this field, a problem has been found, and an error is thrown directly information ; without going down to get the values ​​of other fields, the above picture may be easier to understand:

        PS: It can be seen that the first field in the xxxEntity entity object is staffId.


 Solution:

        The solution is actually very simple, which is to add null judgment logic to the parameter object, and then execute the dao operation, as follows:

Summarize:

        The probability of this error log actually appearing is very low, because when we execute the dao operation, we often receive the request body from the front end through the dto object, and the dto object basically does not pass the null object directly, but a certain object of the object The field value may be a null value; moreover, the required fields of the dto object are basically checked for parameters. The probability of this error log actually appearing is very low due to a combination of factors, but we still need to pay attention.

Guess you like

Origin blog.csdn.net/weixin_42585386/article/details/130218377