java.lang.NullPointerException
at dao.BulletinDaoImpl.delete(BulletinDaoImpl.java:46)
老是这个错误,知道是为空值,但是不知道在哪里解决Servlet public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { request.setCharacterEncoding("gbk");
Long id =  Long.parseLong(request.getParameter("id"));
BulletinDao bulletinDao = new BulletinDaoImpl();
bulletinDao.delete(bulletinDao.get(id));
response.sendRedirect("FindServlet");
}delete方法 public void delete(Bulletin bulletin) {
try {
session = this.getSession();
tx.begin();
session.delete(bulletin);
tx.commit();
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
}

}get方法 public Bulletin get(Long id) {
try {
session = this.getSession();
String hql = "from Bulletin where id=?";
Query query = session.createQuery(hql);
query.setLong(0, id);
return (Bulletin)query.uniqueResult();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
在页面上传递一个ID过来,已经测试有ID传过来,用的是Hibernate框架, 求解答。谢谢

解决方案 »

  1.   

    调试下你的get(Long id)方法是不是返回null如果是的话改成这样
    public Bulletin get(Long id) {
     session = this.getSession();
     Bulletin bulletin=null;
     String hql = "from Bulletin where id=?";
     try {
     Query query = session.createQuery(hql);
     query.setLong(0, id);
     bulletin = (Bulletin)query.uniqueResult();
     } catch (Exception e) {
     e.printStackTrace();
     }
     return bulletin;
     }
      

  2.   

    先确定bulletinDao.get(id) 是不是为空啊  没准 你的id 就是为空呢
      

  3.   

    应该不是这里的错误。报的是BulletinDaoImpl 46行的错。
    把项目发来膜拜一下可以不?[email protected]
      

  4.   

    过滤下bulletinDao.get(id)为空就不执行
    if(bulletinDao.get(id)!=null && !bulletinDao.get(id).equals("") ){
    bulletinDao.delete(bulletinDao.get(id));
    }