0

Currently I have an java object called work, that object has this method inside of it.

public void Connection(Connection conn) throws NamingException, SQLException {


    // Setup the Database datasource
    Context ctx = new InitialContext();
    Context env = (Context) ctx.lookup("java:comp/env");
    DataSource ds = (DataSource) env.lookup("jdbc/carRentalSystem");
    conn = ds.getConnection();
}

I am calling this method inside of my servlet to connect to my DB and add a table row. For what ever reason I keep getting a null pointer exception when running it through debug. Can anyone guide me how to get this to work properly?

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    // Obtain submitted form data

    Work work = new Work();
    String firstName = req.getParameter("First_Name");
    String lastName = req.getParameter("Last_Name");
    String username = req.getParameter("User_Name");
    String email = req.getParameter("Email_Address");
    String password = req.getParameter("Password");

    ResultSet rs = null;
    Connection conn = null;
    Statement st = null;

    try {
        work.Connection(conn);


        // Prepare the SQL statmenet to insert the values

        PreparedStatement stmt = conn.prepareStatement("INSERT INTO userdetails(First_Name, Last_Name, Email_Address, Password, User_Name)  VALUES (?,?,?,?,?)");
        stmt.setString(1, firstName);
        stmt.setString(2, lastName);
        stmt.setString(3, email);
        stmt.setString(4, password);
        stmt.setString(5, username);

        // Execute the insert
        stmt.executeUpdate();
        conn.close();

        // Dispatch into success page
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("login.jsp");
        requestDispatcher.forward(req, res);
    } catch (Exception e) {
        work.ErrorHome(req, res, e);
    } finally {
        try {
            if (st != null)
                st.close();
        } catch (java.sql.SQLException e) {
        }
        try {
            if (conn != null)
                conn.close();
        } catch (java.sql.SQLException e) {
        }
        try {
            if (rs != null)
                rs.close();
        } catch (java.sql.SQLException e) {
        }
    }
}

}

  • Add stacktrace to help – BoBasket4 Oct 12 '17 at 12:34
  • 1
    Are we supposed to guess where exactly you are getting the NullPointerException or why aren't you telling us and posting the Stacktrace? beside that i really doubt that this is different from a typical NPE and will therefor probably be just closed as a duplicate. – OH GOD SPIDERS Oct 12 '17 at 12:34
  • Did you write the method `public void Connection(Connection conn)` yourself? Because it will not work because [Java is allways pass by value](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). You need to return the created connection and work with that return value! – OH GOD SPIDERS Oct 12 '17 at 12:39
  • I did write this my self. and thank you for that. –  Oct 12 '17 at 12:40

1 Answers1

1

Rewrite your Connection(Connection conn) method as follows:

public Connection createConnection() throws NamingException, SQLException {
    // Setup the Database datasource
    Context ctx = new InitialContext();
    Context env = (Context) ctx.lookup("java:comp/env");
    DataSource ds = (DataSource) env.lookup("jdbc/carRentalSystem");
    return ds.getConnection();
}

And later on, in your source code use:

conn = work.createConnection();

instead of:

work.Connection(conn);

This should avoid your NPE.

P3trur0
  • 3,155
  • 1
  • 13
  • 27