添加學生那個在頁面上不能添加 ,自己寫了一個添加的方法,為什么總是報空指針異常?
ERROR org.apache.struts2.dispatcher.DefaultDispatcherErrorHandler - Exception occurred during processing request: null
java.lang.NullPointerException
?? ?at service.impl.StudentsDAOImpl.addStudents(StudentsDAOImpl.java:64) ~[StudentsDAOImpl.class:?]
?? ?at action.StudentsAction.add(StudentsAction.java:51) ~[StudentsAction.class:?]
2019-07-05
感謝分享
2017-06-19
你可以參考一下:把下面的代碼內容,跟你寫的做一下對比
一、在StudentsDAOImpl.java中實現添加學生的方法即可
//添加學生 ?????public?boolean?addStudents(Students?s)?{ ??????????//因為添加的學生是沒有學號的,所以我們需要添加一個學號 ??????????s.setSid(getNewSid()); ??????????Transaction?tx?=?null; ??????????try{ ??????????????Session?session?=?MyHibernateSessionFactory.getSessionFactory().getCurrentSession(); ??????????????tx?=?session.beginTransaction(); ??????????????session.save(s); ??????????????tx.commit(); ??????????????return?true; ??????????}catch(Exception?ex){ ??????????????ex.printStackTrace(); ??????????????tx.commit(); ??????????????return?false; ??????????}finally{ ??????????????if(tx?!=?null){ ???????????????????tx?=?null; ??????????????} ??????????} ?????}二、StudentsAction類中add()方法:
//添加學生 ?????public?String?add()?throws?Exception{ ??????????Students?s?=?new?Students(); ??????????s.setSname(request.getParameter("sname")); ??????????s.setGender(request.getParameter("gender")); ??????????SimpleDateFormat?sdf?=?new?SimpleDateFormat("yyyy-MM-dd"); ??????????s.setBirthday(sdf.parse(request.getParameter("birthday"))); ??????????s.setAddress(request.getParameter("address")); ??????????StudentsDAO?sdao?=?new?StudentsDAOImpl(); ??????????sdao.addStudents(s); ??????????return?"add_success"; ?????}三、struts.xml
<package?name="students"?namespace="/students"?extends="default"> ??????????<action?name="*_*"?class="action.{1}Action"?method="{2}"> ??????????????<result?name="query_success">/students/Students_query_success.jsp</result> ??????????????<result?name="add_success">/students/Students_add_success.jsp</result> ??????????????<result?name="modify_success">/students/Students_modify.jsp</result> ??????????????<result?name="delete_success"?type="chain">Students_query</result><!--?chain表示后面鏈接一個動作,也就是服務器內部轉發,它轉發的只能是action,而不是一個jsp頁面?--> ??????????</action> ?????</package>四、Students_add.jsp中的form表單