做一个通讯录后台管理系统 在TOMCAT上面报错是
java.lang.NullPointerException
com.gem.comm.zhuceDaoImpl.通讯录首页(zhuceDaoImpl.java:21)
com.gem.comm.zhuceServlet.doGet(zhuceServlet.java:24)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
我实现接口的代码是
public class zhuceDaoImpl implements zhuceDao {
public zhucebiao 通讯录首页(String name, String pwd){
Connection conn = null;
PreparedStatement psta = null;
ResultSet rs=null;
zhucebiao zhuce = null;
try {
conn = DBUtil.getConnection();
String sql = "select * from zhucebiao where zhanghao=? and mima=?";
psta = conn.prepareStatement(sql);
psta.setString(1, name);
psta.setString(2, pwd);
rs = psta.executeQuery();
while(rs.next()){
String userName = rs.getString("zhanghao");
String password = rs.getString("mima");
zhuce = new zhucebiao();
zhuce.setZhanghao(userName);
zhuce.setMima(password);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.closeAll(null, psta, conn);
}
return zhuce;
}
}
servlet 代码是
public class zhuceServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
String name = request.getParameter("userName");
String password = request.getParameter("pwd");
System.out.println(name);
System.out.println(password);
zhuceDao zhuDao = new zhuceDaoImpl();
zhucebiao zhuce = zhuDao.通讯录首页(name, password);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
if(zhuce == null){
out.println("对不起您的用户名密码错误~~");
}else{
out.println("欢迎登陆~~");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
哪位大哥帮帮忙 感激不尽 才开始学 每天最开心的就是看见代码运行是成功的 泪流满面
------解决方案--------------------------------------------------------
com.gem.comm.zhuceDaoImpl.通讯录首页(zhuceDaoImpl.java:21)
找找21行,这里出现了空指针异常。建议:不要用中文做为方法名或者类名。在String password = rs.getString("mima");后把userName和password打印出来看下值是多少。
------解决方案--------------------------------------------------------
首先,函数名不要用中文啊,这是最起码的规范,
java.lang.NullPointerException
com.gem.comm.zhuceDaoImpl.通讯录首页(zhuceDaoImpl.java:21)
这个报的是空指针异常,你可以看他已经提示了具体的文件和在哪一行,可以到文件那一行去查看一下;
你这个地方里面的两个变量'name'和'pwd'在那边赋值的了?
psta.setString(1, name);
psta.setString(2, pwd);
------解决方案--------------------------------------------------------
出了错,不要怕,要学会看异常信息。
不要一味的百度,只会让人变傻。自己要学会分析、解决问题。
首先,函数名不要用中文啊,这是最起码的规范,
java.lang.NullPointerException
com.gem.comm.zhuceDaoImpl.通讯录首页(zhuceDaoImpl.java:21)
异常信息已经基本上可以定位到错误在哪里了。