DAO类中的NullPointerException

时间:2016-04-18 05:58:33

标签: java nullpointerexception

有人知道我为什么退回此错误吗?

我的课程是:

conexionDB:

public class ConexionDB {
    private Connection cnn;

    private static ConexionDB instance;

    private ConexionDB() {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/control?useSSL=false", "root", "root");
        } catch (ClassNotFoundException | SQLException e) {

            e.printStackTrace();
        }
    }

    public static ConexionDB estado() {
        if (instance == null) {
            instance = new ConexionDB();
        }
        return instance;
    }

    public Connection getCnn() {
        return cnn;
    }

    public void cerraConexion() {
        instance = null;
    }

}

的Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  String error;
  String email = request.getParameter("email");
  String password = request.getParameter("password");
  HttpSession session = request.getSession();
  EmpleadosDAO empleadoDAO = new EmpleadosDAO();
  EmpleadosDTO userName = empleadoDAO.login(new EmpleadosDTO(email, password));


if (userName == null) {
   error = "Invalid Email or password";
   session.setAttribute("error", error);
   response.sendRedirect("index.jsp");
} else {
   session.setAttribute("user", userName);
   response.sendRedirect("welcome.jsp");
}

DAO:

private final String SQL_LOGIN = "SELECT email from empleados where email=? and password=?";
private ConexionDB con = ConexionDB.estado();

public EmpleadosDTO login(EmpleadosDTO c) {

    java.sql.PreparedStatement ps;
    ResultSet res;
    EmpleadosDTO l = null;

    try {

        ps = con.getCnn().prepareStatement(SQL_LOGIN);

        ps.setString(1, c.getEmail());
        ps.setString(2, c.getPassword());

        res = ps.executeQuery();
        while (res.next()) {
            l = new EmpleadosDTO(res.getString(1), res.getString(2));
        }
        return l;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        con.cerraConexion();
    }
        return l;
}

错误:

  

abr 18,2016 12:25:42 AM org.apache.catalina.core.StandardWrapperValve   调用Grave:Servlet.service()para servletLoginControllerlanzó   excepciónjava.lang.NullPointerExceptionat   com.controlventas.dao.EmpleadosDAO.login(EmpleadosDAO.java:29)at at   com.controlventas.controller.ClienteCONTROLLER.doGet(ClienteCONTROLLER.java:44)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:621)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:722)at at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)     在   org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:851)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)     在   org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:278)     在   org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:515)     在   org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:302)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:615)     在java.lang.Thread.run(Thread.java:744)

0 个答案:

没有答案