qq_39410441
她是月光好悲凉
2017-07-05 14:48

关于eclispe中登录验证的空指针异常

  • arraylist
  • 数据库
  • 指针

UserDao.java 页面

package dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import po.User;
public class UserDao {
public List getUser() {
try {
Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序

Connection connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/jsptest","root","123456");
//连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from user");
List list = new ArrayList();
while (rs.next()){
list.add(new User(rs.getInt("id"),rs.getString("username"),rs.getString("password"),rs.getInt("power")));
}
return list;
}
catch (Exception e) {
System.out.print("get data error!");
e.printStackTrace();
return null;
}
}
}

LoginServlet.java页面

package myservlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDao;
import po.User;

public class LoginServlet extends HttpServlet{

private static final long serialVersionUID = 1L;
public LoginServlet(){
    super();
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    UserDao userdao=new UserDao();
    List<User> userlist=new ArrayList<User>();
    userlist=userdao.getUser();
    for(User l:userlist){
        if(l.getUsername().equals(username)&&l.getPassword().equals(password)){
            request.getRequestDispatcher("a.jsp").forward(request, response);
        }else{
            request.getRequestDispatcher("b.jsp").forward(request, response);
        }

    }


}

}

报错:
严重: Servlet.service() for servlet LoginServlet threw exception
java.lang.NullPointerException
at myservlet.LoginServlet.doPost(LoginServlet.java:34)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:610)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:503)
at java.lang.Thread.run(Unknown Source)

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答

为你推荐