could not instantiate class [xxx] from tuple

来源:互联网 发布:淘宝心选在哪里显示 编辑:程序博客网 时间:2024/06/07 05:31

错误代码提示:

org.hibernate.QueryException: could not instantiate class [com.example.cms.model.Attachment] from tuple    ... moreCaused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)    ... moreCaused by: java.lang.NullPointerException    ... more

原因分析:
欲查询的对象A中,和对象B关联,在A的一个构造函数中,使用的不是B对象,而是B的id字段,类似如下格式:

public A(field a1, ... ,int bid){    this.a1 = a1;    ...    this.b.setId(bid);}

此时会出现上述报错,报错的三处caused by翻译过来分别是:
1、没有匹配的构造函数
2、反射对象异常
3、空指针异常

原因出在this.b.setId(bid)身上,由于实例化A的时候,此时A.b还是null,若调用this.b.set方法,就会出现NullPointerException。
修改该处构造方法如下:

public A(field a1, ... ,int bid){    this.a1 = a1;    ...    B b = new B(); //此处创建一个b对象    this.b.setId(bid);}

问题解决

0 0
原创粉丝点击