一个登陆界面提交后,查询数据库验证合法性,却出现如下错误
java.lang.NullPointerException
bean.UserLoginBean.login(UserLoginBean.java:42)
servlet.user.UserLoginServlet.doGet(UserLoginServlet.java:41)
servlet.user.UserLoginServlet.doPost(UserLoginServlet.java:58)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
第42行是
UserLoginBean的代码如下:
package bean;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import util.*;
public class UserLoginBean {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
ArrayList al = new ArrayList();
public int login(String name,String pwd,String station){
int temp = 0;
try {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String url="jdbc:sqlserver://localhost:1433;Databasename=sxgl";
String user="sa";
String passsword="523325";
conn=DriverManager.getConnection(url,user,passsword);
st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
if(station.equals("学生")){
rs = st.executeQuery("select * from student where name='"+name+"'");
}
if(station.equals("指导老师") || station.equals("指导班主任") || station.equals("小组带队老师")){
rs = st.executeQuery("select * from teacher where name='"+name+"'and station='"+station+"'");
}
if (station.equals("实习学校")){
rs = st.executeQuery("select * from school where name='"+name+"'");
}
if(rs.next()){
String id = rs.getString("id");
String tname = rs.getString("name");
String tpwd = rs.getString("pwd");
String truename = rs.getString("truename");
String stations = rs.getString("station");
if(pwd.equals(tpwd)){
temp = 1;
al.add(id);
al.add(tname);
al.add(tpwd);
al.add(stations);
al.add(truename);
}else{
temp = 2;
}
}else{
temp = 3;
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
DBConn.close(conn,st,rs);
}
return temp;
}
public ArrayList getArrayLst(){
return al;
}
}
------解决方案--------------------------------------------------------
你的station看看有没有值,打印一下,最好在加个station.trim();