关于java中向数据库中插入数据时,报错Caused by: java.lang.NullPointerException的问题

今天在实现一个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的获取数据类型。


建议:

在实体类中,最好都是使用包装类,这样可以减少错误的发生。




  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值