Tomcat中的Java NullPointErexception

时间:2012-04-20 22:47:25

标签: java mysql sql servlets jdbc

我正在使用jsp和servlet开发注册系统,当我在本地调试时,我没有得到NullPointerException。但是当我将它部署到远程服务器时,我得到了这个NullPointerException。

我的代码中是否有任何错误。

package com.app.base;

//imported necessary packages. removes for readability.

public class RegisterServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    PrintWriter out = null;
    // Connection connection = null;
    // Statement statement;
    // ResultSet rs;

    resp.setContentType("text/html");
    out = resp.getWriter();

    String fname = req.getParameter("fname");
    String lname = req.getParameter("lname");
    String email = req.getParameter("email");
    String password = req.getParameter("password");
    String city = req.getParameter("city");
    String country = req.getParameter("country");

    Connection connection = null;
    try {
        // Load the JDBC driver
        Class.forName("com.mysql.jdbc.Driver");

        // Create a connection to the database
        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306,/main",
                "root", "root");

    } catch (SQLException ex) {
        resp.sendRedirect("index.jsp?error=SQLError");
    } catch (ClassNotFoundException e) {
        resp.sendRedirect("index.jsp?error=Class Not Found");
    }

    PreparedStatement statement;
    try {
        statement = connection
                .prepareStatement("INSERT INTO main.users(first_name, last_name, email, password, "
                        + "city, country, registered_time)VALUES(?,?,?,?,?,?, NOW())");
        statement.setString(1, fname);
        statement.setString(2, lname);
        statement.setString(3, email);
        statement.setString(4, password);
        statement.setString(5, city);
        statement.setString(6, country);

        int rs = statement.executeUpdate();

        if (rs == 1) {
            resp.sendRedirect("index.jsp?registered=true");
            // String destination = "index.jsp?registered=true";
            // RequestDispatcher rd =
            // getServletContext().getRequestDispatcher(destination);
            // rd.forward(req, resp);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

        resp.sendRedirect("index.jsp?registered=false");
    }

}

}

stacktrace:

  SEVERE: Servlet.service() for servlet [Register] in context with path [] threw exception
  java.lang.NullPointerException
at com.app.base.RegisterServlet.doPost(RegisterServlet.java:79)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:306)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:322)
at org.apache.tomcat.util.net.AprEndpoint$SocketWithOptionsProcessor.run(AprEndpoint.java:1688)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

2 个答案:

答案 0 :(得分:2)

在所有可能的情况下,找不到驱动程序类,这意味着对Class.forName()的调用会引发ClassNotFoundException。你发送一个重定向(顺便说一句,没有记录异常,这不是一个好主意,你现在已经可以看到你很难找到错误),但你没有从你的电话返回。当您尝试拨打null时,将连接保留为prepareStatement并在代码中稍后导致NPE。

发送重定向后停止通话,添加return ;

当然,您应该找到这样或那样的方法在Tomcat上提供该驱动程序。

答案 1 :(得分:0)

你错误地把额外的东西放在你的字符串中

connection = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306,/main",
            "root", "root");

注意,/main

而应该是

connection = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/main",
            "root", "root");

对不起如果这不是正确答案,我不是像上面所有人一样的专家