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

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

2011-12-29 
空指针错误...折磨了在下好几天了救命啊啊啊啊!!!大侠们我做的是很简单的Struts+Hibernate现在刚开始 插入

空指针错误...折磨了在下好几天了 救命啊啊啊啊!!!大侠们
我做的是很简单的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(){


try{ Session s=HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
List<?> results=s.createQuery("from Student stu").list();
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
if(results!=null&&results.size()>0){
return results;
}
}catch(HibernateException e){
log.fatal(e);
}
return null;  
}
public boolean deleteStudentByID(String id){
try{
Session s=HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
Student stu=(Student)s.load(Student.class, id);
HibernateUtil.commitTransaction();
HibernateUtil.beginTransaction();
s.delete(stu);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
return true;
}catch(HibernateException e){
log.fatal(e);
}
return false;
}
public Student getStudentByID(String id){
try{Session s=HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
Student stu=(Student)s.load(Student.class, id);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
return stu;
}catch(HibernateException e){
log.fatal(e);
}
return null;  
}public boolean saveStudent(Student stu){
try{Session s=HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
s.saveOrUpdate(stu);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
return true;
}catch(HibernateException e){
log.fatal(e);
}
return false;
 

}
   




 

[解决办法]

Java code
public class StuRegAction extends Action{  private StudentDAO stuDao;  public StudentDAO getStudentDAO(){        if(DAOFactory.getInstance()==null)             System.out.println("--------DAOFactory.getInstance()==null-----------");        if(DAOFactory.getInstance().createStudentDAO()==null)             System.out.println("DAOFactory.getInstance().createStudentDAO()==null");  return DAOFactory.getInstance().createStudentDAO();}
[解决办法]
return DAOFactory.getInstance().createStudentDAO();
绝对的就是这句返回了null对象啦
检查你createStudentDAO()方法,debug下就很清楚啦
[解决办法]
private StudentDAO stuDao=new StudentDAO();
应该就没错了,要不就是没返回值,分层测试。

热点排行