我想在数据库中使用hibernate和struts2.But持久保存一个实体“Doctor”。每次按下按钮保存“医生”表格它会抛出空指针异常。我检查了一千次但是我不知道我在做什么错。
这是我的实体
@Entity
public class Doctor implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String firstName;
private String lastName;
private String email;
public Doctor() {
}
public Doctor(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
}
这是我的ActionSupport类,它试图在数据库中保留医生
public class DoctorsController extends ActionSupport implements ModelDriven<Doctor>
{
private DoctorDao docDao = new DoctorDaoImpl();
private List<Doctor> listDoc = new ArrayList<Doctor>();
private Doctor doc = new Doctor();
public List<Doctor> getListDoc() {
return listDoc;
}
public void setListDoc(List<Doctor> listDoc) {
this.listDoc = listDoc;
}
public String excute() throws Exception {
return SUCCESS;`
}
public String getDoctorView() {
return SUCCESS;
}
public Doctor getDoc() {
return doc;
}
public void setDoc(Doctor doc) {
this.doc = doc;
}
public String addDoctor() {
this.docDao.addDoctor(this.doc);
listDoc = docDao.listDoctor();
return SUCCESS;
}
@Override
public Doctor getModel() {
return doc;
}
public DoctorDao getDocDao() {
return docDao;
}
public void setDocDao(DoctorDao docDao) {
this.docDao = docDao;
}
}
这是我医生的表格
<html>
<head>
</head>
<body>
<s:form action="saveDoctor" method="post">
<table>
<s:push value="doc">
<s:hidden name="id" />
<s:textfield name="firstName" label="First Name" />
<s:textfield name="lastName" label="Last Name" />
<s:textfield name="email" label="Email" />
</table>
<input type="submit" value="submit">
</s:push>
</s:form>
</body>
</html>
<struts>
<package name="default" extends="struts-default">
<action name="index" class="Controller.DoctorsController">
<result name="success">/doctor.jsp</result>
</action>
<action name="addDoctorView" class="Controller.DoctorsController">
<result name="success">/jsp/doctor.jsp</result>
</action>
<action name="saveDoctor" class="Controller.DoctorsController" method="addDoctor">
<result name="success">/jsp/doctorlist.jsp</result>
</action>
</package>
</struts>
每当我想坚持医生时,它就会抛出Nullpointer Exception。