日期:2014-05-20  浏览次数:20959 次

空指针异常
在提交的servlet里出现异常
ACCESS数据库
id自动获取
password文本类型
测试id,password值能传递过来
不明白为什么
String   query= "select   *   from   customer   where   password= "+pwd; //这样是空指针异常
String   query= "select   *   from   customer   where   id= ' "+id+ " '   and   password= ' "+pwd+ " ' ";//这样也是空指针异常
-----------------------------
<%@page   contentType= "text/html;charset=gb2312 "   language= "java "   import= "java.sql.* "   errorPage= " "%>
<html>
<head> <title> 用户登陆 </title> </head>
<body>
用户登陆
<form   method= "post "   action= "/jsp/UserLogin ">
<%
if(request.getAttribute( "errInf ")!=null){
%>
<%=(String)request.getAttribute( "errInf ")%>
<%}%>
帐号 <input   type= "text "   name= "id ">
密码 <input   type= "password "   name= "password "> </p>
<input   type= "submit "   value= "提交 ">
<a   href= "/jsp/changePassword.jsp "> 返回 </a>
</body>
</html>
---------------------
UserLogin
import   java.io.*;
import   java.sql.*;
import   javax.servlet.*;
import   javax.servlet.http.*;
import   bean.DBClass;
import   bean.StrClass;
import   bean.UserInf;
public   class   UserLogin   extends   HttpServlet{
public   void   doPost(HttpServletRequest   request,HttpServletResponse   response)throws   ServletException,IOException{
HttpSession   session=request.getSession();
String   toJsp= "/hasLogin.jsp ";
if(session.getAttribute( "userId ")==null){
String   id=request.getParameter( "id ");
String   pwd=request.getParameter( "pwd "); //String   query= "select   *   from   customer   where   id= "+id;     这样就可以正常登陆
//String   query= "select   *   from   customer   where   password= "+pwd; 这样是空指针异常
String   query= "select   *   from   customer   where   id= ' "+id+ " '   and   password= ' "+pwd+ " ' ";//这样也是空指针异常
DBClass   db=new   DBClass();
db.connect();
ResultSet   resultset=db.executeQuery(query);
try{
if(resultset.next()){
session.setAttribute( "userid ",id);
toJsp= "/loginSuccess.jsp ";
}
else{
toJsp= "/userLogin.jsp ";
request.setAttribute( "errIn ", "*密码与帐号不匹配 ");
}
}
catch(SQLException   sqle){
System.err.println( "Erro   width   connection: "+sqle);
}
db.closeConnection();
}
RequestDispatcher   dispatcher=request.getRequestDispatcher(toJsp);
dispatcher.forward(request,response);
}
}

------解决方案--------------------
String query= "select * from customer where password= "+pwd; //这样是空指针异常
String query= "select * from customer where id= ' "+id+ " ' and password= ' "+pwd+ " ' ";//这样也是空

这样肯定不会空指针异常的!!!
把你的错误信息贴出来。