There was an error in implementing an update data today.
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: java.lang.NullPointerException ### Cause: java.lang.NullPointerException at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:364) at com.sun.proxy.$Proxy31.update(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:250) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:54) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52) at com.sun.proxy.$Proxy65.update(Unknown Source) at com.bigaoread.platform.service.secondarysell.v1.YzPresentPosterService.insertPoster(YzPresentPosterService.java:58) at com.bigaoread.platform.web.manage.secondarysell.v1.YzPresentPosterController.save(YzPresentPosterController.java:142) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498)
......
Caused by: java.lang.NullPointerException at com.bigaoread.platform.model.secondarysell.v1.YzPresentPoster.getCreateTime(YzPresentPoster.java:42) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498)
The sql in my Mybatis file is like this:
<update id="update" parameterType="YzPresentPoster"> UPDATE <include refid="tableName"/> <set> <if test="presentId != null">present_id=#{presentId},</if> <if test="posterName != null">poster_name=#{posterName},</if> <if test="createTime != null">create_time=#{createTime},</if> <if test="updateTime != null">update_time=#{updateTime},</if> <if test="optUser != null">opt_user=#{optUser}</if> </set> WHERE id = #{id} </update>
My service code:
if(YzPresentPosterExist!=null){ yzPresentPosterMapper.update(yzPresentPoster); }else{ yzPresentPoster.setCreateTime(millis); yzPresentPosterMapper.insert(yzPresentPoster); }
My bean entity file:
....
//Creation time private Long createTime; //Update time private Long updateTime;
...
public long getCreateTime() { return createTime; } public void setCreateTime(long millis) { this.createTime = millis; } public long getUpdateTime() { return updateTime; } public void setUpdateTime(long updateTime) { this.updateTime = updateTime; }
....
As you can see from the above entity class, when I declare this createTime attribute, I use Long objects, and then use get and set methods as long types!
Analysis:
When I update the operation, the creation time createTime value is empty into the bean object, and all other attributes have values, only createTime is empty.
1. The createTime property itself is a Long object, and my get method is to get a long basic data type, which is a null pointer exception caused by type mismatch.
2. Why the updateTime attribute is the object and the basic type obtained by the get method has no error? This is because updateTime has value when new operation is performed, and after java 5.0, automatic packing and automatic unpacking are introduced.
Long belongs to the packaging class of long, which automatically converts between them at this time, so when there is value, it automatically converts.
3. If the property of createTime is also the basic type long, then no null pointer exception will be reported.
4. Generally speaking, when building entity classes, it is not a problem to directly use software to generate get and set methods. My mistake is that I modified the properties of createTime halfway and forgot to modify the data types of get and set.
Recommendation:
In entity classes, it is best to use wrapper classes, which can reduce the occurrence of errors.