首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

请问下:级联保存NullPointerException异常

2012-01-14 
请教下:级联保存NullPointerException错误级联保存的时候出现java.lang.NullPointerExceptionselectCourse

请教下:级联保存NullPointerException错误
级联保存的时候
出现java.lang.NullPointerException
selectCourse.setStudentInfo(studentInfo);//从此处开始出错
也就是说根本没保存进去,
System.out.println("学号:" + getSelectSnu() + "教师编号:"
+ getSelectTearcher() + "课程编号:" + getSelect1Course());
这个几个的值已经全部获得了的。
POJO:

Java code
public class SelectCourse implements java.io.Serializable {    // Fields    private String snu;    private TearcherInfo tearcherInfo;    private StudentInfo studentInfo;    private CouresInfo couresInfo;    private String ctime;    private Integer cgrade;/*get、set方法省略*/

Action:
Java code
public String addSelectCourse() throws Exception {        TearcherInfo tearcherInfo = new TearcherInfo();        StudentInfo studentInfo = new StudentInfo();        CouresInfo couresInfo = new CouresInfo();        System.out.println("学号:" + getSelectSnu() + "教师编号:"                + getSelectTearcher() + "课程编号:" + getSelect1Course());        tearcherInfo.setTnu(getSelectTearcher());        studentInfo.setSnu(getSelectSnu());        couresInfo.setCnu(getSelect1Course());        selectCourse.setStudentInfo(studentInfo);//从此处开始出错!java.lang.NullPointerException        selectCourse.setCouresInfo(couresInfo);        selectCourse.setTearcherInfo(tearcherInfo);        selectCourse.setCgrade(getCgrade());        selectCourse.setCtime(getCtime());        try {            adm.saveSelectCourse(selectCourse);        } catch (Exception e) {            return "input";        }        return "success";    }

配置文件:
XML code
<hibernate-mapping>    <class name="com.yaxing.domain.SelectCourse" table="SelectCourse" schema="dbo" catalog="Student">        <id name="snu" type="java.lang.String">            <column name="Snu" length="16" />            <generator class="assigned" />        </id>        <many-to-one name="tearcherInfo" class="com.yaxing.domain.TearcherInfo" fetch="select">            <column name="Tnu" length="10" not-null="true" />        </many-to-one>        <many-to-one name="studentInfo" class="com.yaxing.domain.StudentInfo" update="false" insert="false" fetch="select">            <column name="Snu" length="16" not-null="true" unique="true" />        </many-to-one>        <many-to-one name="couresInfo" class="com.yaxing.domain.CouresInfo" fetch="select">            <column name="Cnu" length="10" not-null="true" />        </many-to-one>        <property name="ctime" type="java.lang.String">            <column name="Ctime" length="10" />        </property>        <property name="cgrade" type="java.lang.Integer">            <column name="Cgrade" />        </property>    </class></hibernate-mapping>

DAO:
Java code
public void save(SelectCourse transientInstance) {        log.debug("saving SelectCourse instance");        try {            getSession().beginTransaction();            getSession().save(transientInstance);            log.debug("save successful");        } catch (RuntimeException re) {            log.error("save failed", re);            throw re;        } 



[解决办法]
探讨

selectCourse是一个POJO类,但在你进行存储操作的时候并没有看到有实例化这个selectCourse对象,肯定就引发NullPointerException异常。

[解决办法]
selectCourse.setStudentInfo(studentInfo);//从此处开始出错!java.lang.NullPointerException
你这里set的是studentinfo对象,这个对象必须已经产生主键值,
解决方法是
Long id=save(studentinfo);//获取插入数据库后返回的主键值
StudentInfo student=dao.getStudentinfobyid(id);//根据主键获得对象
selectCourse.setStudentInfo(student);//
[解决办法]
studentInfo没有主键,你呐个studentinfo应该是从数据库里查出来而不是你自己new一个

热点排行