java.lang.NullPointerException
at sunyang.dao.BaseDAO.getSession(BaseDAO.java:18)
at sunyang.dao.impl.ForumDAO.countBack(ForumDAO.java:94)
at sunyang.dao.impl.ReforumDAO.delete(ReforumDAO.java:86)
at sunyang.service.ForumService.deleteReforum(ForumService.java:82)
at sunyang.action.ForumAction.deleteReforum(ForumAction.java:141)
下面是按报错的顺序的代码
- Java code
public class BaseDAO { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public Session getSession() { Session session = sessionFactory.openSession(); return session; }}
- Java code
public Integer countBack(int forum_id) { Session session = getSession(); String hql = "select forum.countback from Forum forum where forum.id=?"; Query query = session.createQuery(hql); query.setParameter(0, forum_id); List coutbacklist = query.list(); session.close(); int coutback = (Integer) coutbacklist.get(0); return coutback; }
- Java code
public boolean delete(int reforum_id, int forum_id) { try { String hql = "delete from Reforum reforum where reforum.id="+reforum_id; Session session = getSession(); Transaction tx = session.beginTransaction(); Query querydeleteReforum = session.createQuery(hql); int b=querydeleteReforum.executeUpdate(); tx.commit(); session.close(); ForumDAO fdao = new ForumDAO(); int countback = fdao.countBack(forum_id); fdao.removeCountBack(countback, forum_id); if (b != 0) { return true; } else { return false; } } catch (Exception e) { e.printStackTrace(); } return false; }
- Java code
public void deleteReforum(int reforum_id, int forum_id) { try { rfdao.delete(reforum_id, forum_id); } catch (Exception e) { e.printStackTrace(); } }
- Java code
public String deleteReforum() { try { //Map request = (Map) ActionContext.getContext().get("request"); forumservice.deleteReforum(Integer.parseInt(request.getParameter("reforum_id")), Integer.parseInt(request.getParameter("forum_id"))); forum_id = Integer.parseInt(request.getParameter("forum_id")); } catch (Exception e) { e.printStackTrace(); } return "success"; }
其他的action也是这么用的没问题,就这个有问题,比较郁闷,这个
- Java code
public Session getSession() { Session session = sessionFactory.openSession(); return session; }
Session是从sessionFactory得到的,这个异常是sessionFactory里产生的?
------解决方案--------------------
- Java code
public Session getSession() {System.out.println(sessionFactory==null?"true":"false"); Session session = sessionFactory.openSession();System.out.println(session==null?"true":"false"); return session; }