当前位置: 代码迷 >> J2EE >> 空指针异常.折磨了在下好几天了 啊大侠们
  详细解决方案

空指针异常.折磨了在下好几天了 啊大侠们

热度:204   发布时间:2016-04-22 02:51:26.0
空指针错误...折磨了在下好几天了 救命啊啊啊啊!!!大侠们
我做的是很简单的Struts+Hibernate 现在刚开始 插入数据库的操作 一直不行
出错的界面如下:
exception:

javax.servlet.ServletException: java.lang.NullPointerException
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


root cause:

  java.lang.NullPointerException
com.sdut.struts.action.StuRegAction.getStudentDAO(StuRegAction.java:16)
com.sdut.struts.action.StuRegAction.execute(StuRegAction.java:39)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

我按照书的例子来做的 就是填写一个表单 存储到数据库里去

表单的Action是 StuRegAction(学生注册):


package com.sdut.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.sdut.domain.Student;
import com.sdut.struts.form.StuRegForm;
import com.sdut.dao.StudentDAO;
import com.sdut.dao.DAOFactory;
public class StuRegAction extends Action{
  private StudentDAO stuDao;
  public StudentDAO getStudentDAO(){
return DAOFactory.getInstance().createStudentDAO();
  }

public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response){
StuRegForm stuRegForm=(StuRegForm)form;
Student stu= new Student();
String username=stuRegForm.getUsername();
String name=stuRegForm.getName();
String password=stuRegForm.getPassword();
String sex=stuRegForm.getSex();
String tel=stuRegForm.getTel();
String email=stuRegForm.getEmail();
String department=stuRegForm.getDepartment();
String grade=stuRegForm.getGrade();
stu.setUsername(username);
stu.setDepartment(department);
stu.setEmail(email);
stu.setName(name);
stu.setSex(sex);
stu.setPassword(password);
stu.setTel(tel);
stu.setGrade(grade);
stuDao=this.getStudentDAO();
if(!stuDao.saveStudent(stu)){
return mapping.findForward("failure");
} else {
return mapping.findForward("success");
}



}



}

连接数据库有几个相关的文件daoconfig.properties DAOconfig.java DAOfactory.java HibernateDAOFactory.java

其中在DAOfactory.java中 定义了这样的东西:
public abstract StudentDAO createStudentDAO();
在HibernateDAOFactory中 有这样的代码

public StudentDAO createStudentDAO(){
return new StudentDAOImp();
}

StudentDAOImp实现StudentDAO中定义的函数:
package com.sdut.dao;
import java.util.List;
import com.sdut.domain.Student;
public interface StudentDAO {  
List<?> getStudent();
boolean deleteStudentByID(String id);
Student getStudentByID(String id); 
boolean saveStudent(Student stu);

}
StudentDAOImp中是Hibernate访问数据库

public List<?> getStudent(){
  相关解决方案