空指针异常

hailun 2007-03-01 11:06:51
在提交的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);
}
}
...全文
563 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jojoy_828 2007-03-01
  • 打赏
  • 举报
回复
if(session.getAttribute("userId")==null){
这句当中的“userID”你在哪setAttribute了?
hailun 2007-03-01
  • 打赏
  • 举报
回复
String query="select * from customer where password="+pwd; //这样是空指针异常
String query="select * from customer where id='"+id+"' and password='"+pwd+"'";//这样也是空

如是分别用上面这二条语句就是
java.lang.NullPointerException
UserLogin.doPost(UserLogin.java:20)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
错误信息

但是用out.prin(pwd); 却能打印出来 select * from customer where password=888888


out.print(result);//为什么会是null呢
可能是ResultSet resultset=db.executeQuery(query);有异常了。

怎么改呢
hailun 2007-03-01
  • 打赏
  • 举报
回复
java.lang.NullPointerException
UserLogin.doPost(UserLogin.java:20)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
yeah920 2007-03-01
  • 打赏
  • 举报
回复
String query="select * from customer where password="+pwd; //这样是空指针异常
String query="select * from customer where id='"+id+"' and password='"+pwd+"'";//这样也是空

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

out.print(result);//为什么会是null呢
可能是ResultSet resultset=db.executeQuery(query);有异常了。

hailun 2007-03-01
  • 打赏
  • 举报
回复
ResultSet resultset=db.executeQuery(query);
out.print(result);//为什么会是null呢
wwyyhhww 2007-03-01
  • 打赏
  • 举报
回复

DBClass db=new DBClass();
db.connect();
ResultSet resultset=db.executeQuery(query);
出了问题了,你断点查查
wangzhiqing 2007-03-01
  • 打赏
  • 举报
回复
DBClass db=new DBClass();
db.connect();
ResultSet resultset=db.executeQuery(query);

这个需要看看 DBClass 这个类里面的 executeQuery 方法,是怎么写的了。
有可能这个方法,把Exception给截获了,没有抛出来。
因为出错了以后,就返回NULL
hailun 2007-03-01
  • 打赏
  • 举报
回复
在里面呢,楼上的没有仔细看吧

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧