为啥我的jsp页面验证用户名和密码的时候会报空指针错误?代码如下
jsp页面代码:(其中username和password是已经获取到的,抛出的两个异常也是我定义好的)try{Useru=User.userCheck(username,...
jsp页面代码:(其中username和password是已经获取到的,抛出的两个异常也是我定义好的)
try
{
User u=User.userCheck(username,password);
session.setAttribute("user",u);
}catch(UserNotFoundException e)
{
out.println(e.getMessage());
return;
}catch(PasswordNotCorrectException e)
{
out.println(e.getMessage());
return;
}
javabean代码如下:
public static User check(String username, String password)
throws UserNotFoundException, PasswordNotCorrectException {
User u = null;
Connection conn = DB.getConn();
String sql = "select * from user where username = '" + username + "'";
Statement stmt = DB.getStatement(conn);
ResultSet rs = DB.getResultSet(stmt, sql);
try {
if(!rs.next()) {
throw new UserNotFoundException("用户不存在:" + username);
} else {
if(!password.equals(rs.getString("password"))) {
throw new PasswordNotCorrectException("密码不正确哦!");
}
u = new User();
u.setId(rs.getInt("id"));
u.setUsername(rs.getString("username"));
u.setPassword(rs.getString("password"));
u.setPhone(rs.getString("phone"));
u.setAddr(rs.getString("addr"));
u.setRdate(rs.getTimestamp("rdate"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(rs);
DB.close(stmt);
DB.close(conn);
}
return u;
}
报错:java.lang.NullPointerException
at com.shop.user.User.userCheck(User.java:84) (就是if(!rs.next())那一行)
at org.apache.jsp.LoginDeal_jsp._jspService(LoginDeal_jsp.java:64)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 展开
try
{
User u=User.userCheck(username,password);
session.setAttribute("user",u);
}catch(UserNotFoundException e)
{
out.println(e.getMessage());
return;
}catch(PasswordNotCorrectException e)
{
out.println(e.getMessage());
return;
}
javabean代码如下:
public static User check(String username, String password)
throws UserNotFoundException, PasswordNotCorrectException {
User u = null;
Connection conn = DB.getConn();
String sql = "select * from user where username = '" + username + "'";
Statement stmt = DB.getStatement(conn);
ResultSet rs = DB.getResultSet(stmt, sql);
try {
if(!rs.next()) {
throw new UserNotFoundException("用户不存在:" + username);
} else {
if(!password.equals(rs.getString("password"))) {
throw new PasswordNotCorrectException("密码不正确哦!");
}
u = new User();
u.setId(rs.getInt("id"));
u.setUsername(rs.getString("username"));
u.setPassword(rs.getString("password"));
u.setPhone(rs.getString("phone"));
u.setAddr(rs.getString("addr"));
u.setRdate(rs.getTimestamp("rdate"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(rs);
DB.close(stmt);
DB.close(conn);
}
return u;
}
报错:java.lang.NullPointerException
at com.shop.user.User.userCheck(User.java:84) (就是if(!rs.next())那一行)
at org.apache.jsp.LoginDeal_jsp._jspService(LoginDeal_jsp.java:64)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 展开
展开全部
你确定username和password已经从表单获取到了吗?你直接在jsp页面上打印username和password能显示出来吗?测试一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if(!rs.next())你这是判断rs是否有数据集,这样判断不好,我建议你这样判断就可以if(rs == null)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你直接判断结果集是否为空不就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询