日期:2014-05-18  浏览次数:20825 次

一个空指针问题
在login。jsp提交后来到http://localhost:8080/guestbook8/admin/user后,
得到空指针错误:
java.lang.NullPointerException cc415.com.guestbook.AdminUserServlet.login(AdminUserServlet.java:46) cc415.com.guestbook.AdminUserServlet.doGet(AdminUserServlet.java:77) cc415.com.guestbook.AdminUserServlet.doPost(AdminUserServlet.java:87) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) cc415.com.guestbook.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:23)

这此,我用到的AdminUserServlet。java是直接从视频里拿过来的,也就是说视频里刘老师改的那个from已经改好了,我也不知道为什么在那视频里刘老师改了那个from后那个错误就没了。我然后以后是AdminUserServlet。java的46行有问题,就是觉得下面这段有问题

else{
  String param[] =
  { username, password };
  OracleUtil db = new OracleUtil("java:/comp/env/jdbc/oracleds");
  db.init(); List result = (List) db.query(
  "select id from admin where username=? and password=?",
  param, new MapListHandler()); if (result.size() == 0) //该行为我的46行
  {
  message = "对不起,用户名或者密码不正确,请重新输入!";
  request.setAttribute("guestbook.admin.login.message", message);
  request.getRequestDispatcher("/admin/loginFail.jsp").forward(
  request, response);
  } else
  {
  HttpSession session = request.getSession();
  session.setAttribute("guestbook.admin.username", username);
  response.sendRedirect(request.getContextPath()
  + "/admin/secure/manage?q=list");
  }
}

然后我就在46行前判断了一下,改成:

if (result != null)
  {
  if (result.size() == 0)
  {
  message = "对不起,用户名或者密码不正确,请重新输入!";
  request.setAttribute("guestbook.admin.login.message",
  message);
  request.getRequestDispatcher("/admin/loginFail.jsp")
  .forward(request, response);
  } else
  {
  HttpSession session = request.getSession();
  session.setAttribute("guestbook.admin.username", username);
  response.sendRedirect(request.getContextPath()
  + "/admin/secure/manage?q=list");
  }
  }

但接着eclipse却抛出这样一段异常:

2008-10-17 17:52:16 org.apache.catalina.core.StandardContext reload
信息: Reloading this Context has started
java.sql.SQLException: ORA-01008: 并非所有变量都已关联
Query: select id from admin where username=? and password=? Parameters: []
at org.apache.commons.dbutils.QueryRunner.rethrow(QueryRunner.java:359)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:240)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:309)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:328)
at cc415.com.util.OracleUtil.query(OracleUtil.java:64)
at cc415.com.guestbook.AdminUserServlet.login(AdminUserServlet.java:42)
at cc415.com.guestbook.AdminUserServlet.doGet(AdminUserServlet.java:79)
at cc415.com.guestbook.AdminUserServlet.doPost(AdminUserServlet.java:89)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java