public class EditAction extends ActionSupport { /**
*
*/
private static final long serialVersionUID = 1L;
private String subName;
private Byte subType;
private Byte subDifficult;
private String subIntroduction;
private String subUsecomputer;
private String subNo;
private Byte subDirection;
private String subTaskname;
private String subFrom;
private String subReform;
private String subOther;
private Students students;
@Override
public String execute() throws Exception{
try{
SubjectInfoDaoImpl subDao=new SubjectInfoDaoImpl();
Teachers tea ;
HttpServletRequest request = ServletActionContext .getRequest() ;
String subid = request .getParameter("subId") ;
Subjectinfo info = new Subjectinfo();
info=subDao.findById(subid);
// int Id = Integer.parseInt (request.getParameter ("subId"));
// info.setSubId(Id);
HttpSession session = request .getSession() ;
tea = (Teachers) session .getAttribute("teacher") ;
info .setSubName(this .getSubName());
info.setSubType(this .getSubType());
info.setSubDifficult(this.getSubDifficult());//
info .setSubIntroduction(this .getSubIntroduction()) ;//
info.setSubFrom(this.getSubFrom());//
info.setSubTaskname(this.getSubTaskname());//
info .setSubNo(this .getSubNo());//
info .setSubDirection(this .getSubDirection()) ;//
info .setSubReform(this .getSubReform()) ;//
info .setSubOther(this .getSubOther()) ;//
info .setSubUsecomputer(this .getSubUsecomputer()) ;
info .setTeachers(tea);
subDao.updateSubject(info);
return SUCCESS;
}catch(Exception e){
e.printStackTrace();
return ERROR;
}
} public String getSubName() {
return subName;
} public void setSubName(String subName) {
this.subName = subName;
} public Byte getSubType() {
return subType;
} public void setSubType(Byte subType) {
this.subType = subType;
} public Byte getSubDifficult() {
return subDifficult;
} public void setSubDifficult(Byte subDifficult) {
this.subDifficult = subDifficult;
} public String getSubIntroduction() {
return subIntroduction;
} public void setSubIntroduction(String subIntroduction) {
this.subIntroduction = subIntroduction;
} public String getSubUsecomputer() {
return subUsecomputer;
} public void setSubUsecomputer(String subUsecomputer) {
this.subUsecomputer = subUsecomputer;
} public String getSubNo() {
return subNo;
} public void setSubNo(String subNo) {
this.subNo = subNo;
} public Byte getSubDirection() {
return subDirection;
} public void setSubDirection(Byte subDirection) {
this.subDirection = subDirection;
} public String getSubTaskname() {
return subTaskname;
} public void setSubTaskname(String subTaskname) {
this.subTaskname = subTaskname;
} public String getSubFrom() {
return subFrom;
} public void setSubFrom(String subFrom) {
this.subFrom = subFrom;
} public String getSubReform() {
return subReform;
} public void setSubReform(String subReform) {
this.subReform = subReform;
} public String getSubOther() {
return subOther;
} public void setSubOther(String subOther) {
this.subOther = subOther;
} public Students getStudents() {
return students;
} public void setStudents(Students students) {
this.students = students;
}
}
*
*/
private static final long serialVersionUID = 1L;
private String subName;
private Byte subType;
private Byte subDifficult;
private String subIntroduction;
private String subUsecomputer;
private String subNo;
private Byte subDirection;
private String subTaskname;
private String subFrom;
private String subReform;
private String subOther;
private Students students;
@Override
public String execute() throws Exception{
try{
SubjectInfoDaoImpl subDao=new SubjectInfoDaoImpl();
Teachers tea ;
HttpServletRequest request = ServletActionContext .getRequest() ;
String subid = request .getParameter("subId") ;
Subjectinfo info = new Subjectinfo();
info=subDao.findById(subid);
// int Id = Integer.parseInt (request.getParameter ("subId"));
// info.setSubId(Id);
HttpSession session = request .getSession() ;
tea = (Teachers) session .getAttribute("teacher") ;
info .setSubName(this .getSubName());
info.setSubType(this .getSubType());
info.setSubDifficult(this.getSubDifficult());//
info .setSubIntroduction(this .getSubIntroduction()) ;//
info.setSubFrom(this.getSubFrom());//
info.setSubTaskname(this.getSubTaskname());//
info .setSubNo(this .getSubNo());//
info .setSubDirection(this .getSubDirection()) ;//
info .setSubReform(this .getSubReform()) ;//
info .setSubOther(this .getSubOther()) ;//
info .setSubUsecomputer(this .getSubUsecomputer()) ;
info .setTeachers(tea);
subDao.updateSubject(info);
return SUCCESS;
}catch(Exception e){
e.printStackTrace();
return ERROR;
}
} public String getSubName() {
return subName;
} public void setSubName(String subName) {
this.subName = subName;
} public Byte getSubType() {
return subType;
} public void setSubType(Byte subType) {
this.subType = subType;
} public Byte getSubDifficult() {
return subDifficult;
} public void setSubDifficult(Byte subDifficult) {
this.subDifficult = subDifficult;
} public String getSubIntroduction() {
return subIntroduction;
} public void setSubIntroduction(String subIntroduction) {
this.subIntroduction = subIntroduction;
} public String getSubUsecomputer() {
return subUsecomputer;
} public void setSubUsecomputer(String subUsecomputer) {
this.subUsecomputer = subUsecomputer;
} public String getSubNo() {
return subNo;
} public void setSubNo(String subNo) {
this.subNo = subNo;
} public Byte getSubDirection() {
return subDirection;
} public void setSubDirection(Byte subDirection) {
this.subDirection = subDirection;
} public String getSubTaskname() {
return subTaskname;
} public void setSubTaskname(String subTaskname) {
this.subTaskname = subTaskname;
} public String getSubFrom() {
return subFrom;
} public void setSubFrom(String subFrom) {
this.subFrom = subFrom;
} public String getSubReform() {
return subReform;
} public void setSubReform(String subReform) {
this.subReform = subReform;
} public String getSubOther() {
return subOther;
} public void setSubOther(String subOther) {
this.subOther = subOther;
} public Students getStudents() {
return students;
} public void setStudents(Students students) {
this.students = students;
}
}
解决方案 »
- Struts 2 中url中action的写法问题
- spring application
- jxl读写excel时出现的一些格式问题
- JSP页面显示数据不全?加急!
- 请问以下各位高手!tomcat+Jboss的最新版本是多少???
- struts-config.xml里面图形界面怎么让它自动排版哦
- Java支持藏文彝文报表预览,实现技术探讨交流!
- [javax.servlet.ServletException: java.lang.NullPointerException] 一个奇怪的空指针
- tomcat的环境配置问题!
- 实战EJB之四(开发实体CMP),在部署应用程序的时候,点击"实体EJB的持续性管理",”在设置数据源的JNDI名时,如何修改datasources.xml来
- 一条hql语句不明白怎么写
- Struts达人请进:ActionForward的setPath()的困惑
info=subDao.findById(subid);
可能subid=null,所以上面这句就会抛出java.lang.NullPointerException
private Teachers teachers;
private Students students;
private String subName;
private Byte subType;
private Byte subDifficult;
private String subIntroduction;
private String subUsecomputer;
private String subNo;
private Byte subDirection;
private String subTaskname;
private String subFrom;
private String subReform;
private String subOther;
private Set stutemps = new HashSet(0);
// Constructors public Set getStutemps() {
return stutemps;
} public void setStutemps(Set stutemps) {
this.stutemps = stutemps;
...}
<hibernate-mapping>
<class name="hibernate.model.Subjectinfo" table="subjectinfo" catalog="subjectinfo">
<id name="subId" type="java.lang.Integer">
<column name="sub_id" />
<generator class="native" />
</id>
<many-to-one name="teachers" class="hibernate.model.Teachers" fetch="select">
<column name="tea_id" not-null="true" />
</many-to-one>
<many-to-one name="students" class="hibernate.model.Students" fetch="join">
<column name="stu_id" />
</many-to-one>
<property name="subName" type="java.lang.String">
<column name="sub_name" length="100" not-null="true" />
</property>
<property name="subType" type="java.lang.Byte">
<column name="sub_type" not-null="true" />
</property>
<property name="subDifficult" type="java.lang.Byte">
<column name="sub_difficult" not-null="true" />
</property>
<property name="subIntroduction" type="java.lang.String">
<column name="sub_introduction" length="1000" not-null="true" />
</property>
<property name="subUsecomputer" type="java.lang.String">
<column name="sub_usecomputer" length="10">
<comment>涓???烘?舵?</comment>
</column>
</property>
<property name="subNo" type="java.lang.String">
<column name="sub_no" length="10" />
</property>
<property name="subDirection" type="java.lang.Byte">
<column name="sub_direction" />
</property>
<property name="subTaskname" type="java.lang.String">
<column name="sub_taskname" length="20" />
</property>
<property name="subFrom" type="java.lang.String">
<column name="sub_from" length="20" />
</property>
<property name="subReform" type="java.lang.String">
<column name="sub_reform" length="20" />
</property>
<property name="subOther" type="java.lang.String">
<column name="sub_other" length="100" />
</property>
<set name="stutemps" inverse="true">
<key>
<column name="sub_id"></column>
</key>
<one-to-many class="hibernate.model.Stutemp"/>
</set> </class>
</hibernate-mapping>
[color=#FF0000]Action
public class EditAction extends ActionSupport {[/color] /**
*
*/
private static final long serialVersionUID = 1L;
private String subName;
private Byte subType;
private Byte subDifficult;
private String subIntroduction;
private String subUsecomputer;
private String subNo;
private Byte subDirection;
private String subTaskname;
private String subFrom;
private String subReform;
private String subOther;
private Students students; @Override public String execute() throws Exception{
try{
SubjectInfoDaoImpl subDao=new SubjectInfoDaoImpl();
Teachers tea ;
HttpServletRequest request = ServletActionContext .getRequest() ;
String subid = request .getParameter("subId") ;
Subjectinfo info = new Subjectinfo(); info=subDao.findById(subid);
HttpSession session = request .getSession() ;
tea = (Teachers) session .getAttribute("teacher") ;
info .setSubName(this .getSubName());
info.setSubType(this .getSubType());
info.setSubDifficult(this.getSubDifficult());//
info .setSubIntroduction(this .getSubIntroduction()) ;//
info.setSubFrom(this.getSubFrom());//
info.setSubTaskname(this.getSubTaskname());//
info .setSubNo(this .getSubNo());//
info .setSubDirection(this .getSubDirection()) ;//
info .setSubReform(this .getSubReform()) ;//
info .setSubOther(this .getSubOther()) ;//
info .setSubUsecomputer(this .getSubUsecomputer()) ;
info .setTeachers(tea); subDao.updateSubject(info);
return SUCCESS;
}catch(Exception e){
e.printStackTrace();
return ERROR;
}
get() set().....
}
public void updateSubject(Object obj)
{
Session session=HibernateUtil.createSession();
Transaction tx=session.beginTransaction();
session.update(obj);
tx.commit();
HibernateUtil.closeSession();
}
页面<body>题目修改页面: <form name="form21" method="post" action="EditeSubject.action">
<table width="950" border="1"> <tr bgcolor="#E3E3E3">
<td width="198" bgcolor="#E6E6E6"><div align="right">题目:</div></td>
<td colspan="5" bgcolor="#E6E6E6"><div align="left">
<label>
<input type="text" name="subName" id="subName" value='<s:property value="%{info.subName}"/>'>
</label>
</div>
<div align="left"></div></td>
</tr>
<tr bgcolor="#E3E3E3">
<td bgcolor="#E6E6E6"><div align="right">题目类型:</div></td>
<td width="151" bgcolor="#E6E6E6"><div align="left">
<label>
<s:if test='info.subType==1'>
<input type="radio" name="subType" value="1" id="subType" checked>
</s:if>
<s:else>
<input type="radio" name="subType" value="1" id="subType" >
</s:else>
工程设计 </label>
</div></td>
<td width="123" bgcolor="#E6E6E6"><div align="left">
<s:if test='info.subType==2'>
<input type="radio" name="subType" value="2" id="subType" checked>
</s:if>
<s:else>
<input type="radio" name="subType" value="2" id="subType" >
</s:else>
工程技术</div></td>
<td width="124" bgcolor="#E6E6E6"><div align="left">
<s:if test="info.subType==3">
<input type="radio" name="subType" value="3" id="subType" checked>
</s:if>
<s:else>
<input type="radio" name="subType"value="3" id="subType" >
</s:else>
实验研究</div></td>
<td width="122" bgcolor="#E6E6E6"><div align="left">
<s:if test='info.subType==4'>
<input type="radio" name="subType" value="4" id="subType" checked>
</s:if>
<s:else>
<input type="radio" name="subType" value="4" id="subType" >
</s:else>
软件开发</div></td>
<td width="192" bgcolor="#E6E6E6"><div align="left">
<s:if test='info.subType==5'>
<input type="radio" name="subType" value="5" id="subType" checked>
</s:if>
<s:else>
<input type="radio" name="subType" value="5" id="subType" >
</s:else>
理论研究</div></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><div align="right">题目难度:</div></td>
<td colspan="2" bgcolor="#E6E6E6"><div align="left">
<s:if test="info.subDifficult==1">
<input type="radio" name="subDifficult" value="1" id="subDifficult" checked>
</s:if>
<s:else>
<input type="radio" name="subDifficult" value="1" id="subDifficult" >
</s:else>
A
<s:if test="info.subDifficult==2">
<input type="radio" name="subDifficult" value="2" id="subDifficult" checked>
</s:if>
<s:else>
<input type="radio" name="subDifficult" value="2" id="subDifficult" >
</s:else>
B </div></td>
<td bgcolor="#E6E6E6"><div align="right">上机时数:</div></td>
<td colspan="2" bgcolor="#E6E6E6"><div align="left">
<label>
<input type="text" name="subUsecomputer" value='<s:property value="%{info.subUsecomputer}"/>'id="subUsecomputer">
</label>
</div></td>
</tr>
<tr bgcolor="#E3E3E3">
<td rowspan="5" bgcolor="#E6E6E6">题目来源于科研课题(该课题的情况)</td>
<td bgcolor="#E6E6E6"><div align="right">课题编号:</div></td>
<td bgcolor="#E6E6E6"><div align="left">
<label>
<input type="text" name="subNo" value='<s:property value="%{info.subNo}"/>'id="subNo">
</label></div></td>
<td bgcolor="#E6E6E6"><div align="right">研究方向:</div></td>
<td bgcolor="#E6E6E6"><div align="left">
<s:if test="info.subDirection==1">
<input type="radio" name="subDirection" value="1" id="subDirection" checked>
</s:if>
<s:else>
<input type="radio" name="subDirection" value="1" id="subDirection" >
</s:else>
横向
</div></td>
<td bgcolor="#E6E6E6"><div align="left">
<s:if test="info.subDirection==2">
<input type="radio" name="subDirection" value="2" id="subDirection" checked>
</s:if>
<s:else>
<input type="radio" name="subDirection" value="2" id="subDirection" >
</s:else>
纵向</div></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><div align="right">课题名称:</div></td>
<td colspan="4" bgcolor="#E6E6E6"><div align="left">
<label>
<input type="text" name="subTaskname" value='<s:property value="%{info.subTaskname}"/>' id="subTaskname">
</label></div></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><div align="right">课题来源:</div></td>
<td colspan="4" bgcolor="#E6E6E6"><div align="left">
<label>
<input type="text" name="subFrom" value='<s:property value="%{info.subFrom}"/>'id="subFrom">
</label></div></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><div align="right">教学、实验改革:</div></td>
<td colspan="4" bgcolor="#E6E6E6"><div align="left">
<label>
<input type="text" name="subReform" value='<s:property value="%{info.subReform}"/>' id="subReform">
</label></div></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><div align="right">其他:</div></td>
<td colspan="4" bgcolor="#E6E6E6"><div align="left">
<label>
<input type="text" name="subOther" value='<s:property value="%{info.subOther}"/>' id="subOther">
</label></div></td>
</tr>
<tr bgcolor="#E3E3E3">
<td colspan="6" bgcolor="#E6E6E6">主要任务与要求简述:</td>
</tr>
<tr bgcolor="#F3F3F3">
<td colspan="6" bgcolor="#E6E6E6">
<label>
<textarea name="subIntroduction" id="subIntroduction" cols="140" rows="5" />${info.subIntroduction}</textarea>
</label>
</td>
</tr>
<tr bgcolor="#F3F3F3">
<td colspan="6" bgcolor="#E6E6E6"><div align="center">
<input name="submit" type="submit" id="submit" value="修改" />
</div></td>
</tr>
</table>
</form>
</body>
错误
==========================================页面传过来的值====================================
subName:我[color=#FFFF00]地方
subType: 1
subUsercomputer:421
subDifficult:1
介绍: 3333
来源:2
编号:2
其他:2
方向:1
课程名:2
改革:2
老师莫金旺===========================================页面传过来的值====================================
=====================================================Teacher的值获取==============================
=====================================================Teacher的值获取==============================
java.lang.NullPointerException[/color]
at struts2.action.teacher.subject.EditAction.execute(EditAction.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
注意这段代码
info .setTeachers(tea);
subDao.updateSubject(info);
注意这个tea应该是teacher吧,看hbm文件是个类型属性,和teacher是多对一关联的
这个tea是从session里拿来的,会是null吗?
如果是null,在执行subDao.updateSubject(info);这段代码的时候
hibernate会反射调用teacher.getid的方法给info的teacherid字段赋值,如果teacher是null,自然就空指针异常了
建议你检查下teacher就是那个tea的值吧