Isuru
Isuru

Reputation: 3958

Java NullPointException on Tomcat

I am developing registration system with jsp and servlet, and when I tun locally I don't get NullPointerException. But when I deploy it into remote server I get this NullPointerException.

Is there any errors in my code.

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");
    }

}

}

The 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)

Upvotes: 0

Views: 887

Answers (2)

Bhavik Shah
Bhavik Shah

Reputation: 5183

You mistakenly put extra , in your string

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

notice the ,/main

instead it should be

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

Sorry If this is not the right answer I am not an expert like all the people above

Upvotes: 0

Guillaume Polet
Guillaume Polet

Reputation: 47608

In all likely hood the driver class is not found, meaning that the call to Class.forName() throws a ClassNotFoundException. You send a redirect (btw, without logging the exception which is not a good idea, you can already see now that you have a hard time finding the error) but you don't return from your call. Leaving the connection to be null and causing the NPE later in your code when you try to call prepareStatement).

Add a return ; after you send a redirect to stop your call.

Of course you should find one way or another to have that driver available on your Tomcat.

Upvotes: 2

Related Questions