今天在实现一个update数据时出现一个错误;
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)
从这个报错信息中,可以看出,在service执行update操作时,还没有执行sql语句就报了一个 NullPointerException异常。
继续看错误,发现是 getCreateTime时报的空指针,说明程序在执行update操作时,需要先取javabean中的数据,然后出现的空指针异常,就是bean实体中
的getCreateTime时空指针。
我的Mybatis文件中中的sql是这样的:
<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>
我的service代码:
if(YzPresentPosterExist!=null){
yzPresentPosterMapper.update(yzPresentPoster);
}else{
yzPresentPoster.setCreateTime(millis);
yzPresentPosterMapper.insert(yzPresentPoster);
}
我的bean实体文件:
....
//创建时间
private Long createTime;
//更新时间
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;
}
....
从上面的实体类可以看出了问题,我在声明这个createTime属性时,使用的Long对象,而再使用get和set方法是long类型!
解析:
我在更新操作时,创建时间createTime值为空存入bean对象,其他属性都有值,只有createTime为空。
1、本身createTime属性为一个Long对象,而我get方法是获取一个long的基本数据类型,这是类型不匹配,导致的空指针异常。
2、为什么updateTime属性为对象,get方法获取的基本类型,却没有报错,这个原因是因为跟新操作时,updateTime时有值,并且在java 5.0后引入了自动装箱与自动拆箱,
Long属于long的包装类,此时它们之间会自动转换,所以有值时,会自动转换。
3、如果createTime的属性也为基本类型long时,此时就不会报空指针异常。
4、一般来说,在构建实体类时,直接利用软件生成get和set方法就不会有问题,本人的错误是因为我中途修改了createTime的属性,而忘了修改get和set的获取数据类型。
建议:
在实体类中,最好都是使用包装类,这样可以减少错误的发生。