-2

java.lang.NullPointerException: Cannot invoke "java.sql.PreparedStatement.setString(int, String)" because "this.statement" is null

Registartion.java

package in.adit.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

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

@WebServlet("/Registration")
public class Registration extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private static final String URL = "jdbc:mysql://localhost:3306/user";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "";

    Connection connection = null;
    PreparedStatement statement = null;

    public Registration() {
        super();
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            System.out.println("Connect established succesfully");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        String un = request.getParameter("username");
        String pass = request.getParameter("password");
        String firstName = request.getParameter("password");
        String lastName = request.getParameter("password");
        String email = request.getParameter("password");
        String m = request.getParameter("mobile");
        System.out.println(m);
        Long mobile = Long.parseLong(m);

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();     
        try {
            String query2 = "INSERT INTO login_tbl VALUES (?,?)";
            //statement = connection.prepareStatement(query2);
            statement.setString(1,un);
            statement.setString(2,pass);
            statement.execute(query2);
            System.out.println("Query 2 Executed...");
            
            String query = "INSERT INTO user_info_tbl VALUES (?,?,?,?,?)";
            //statement = connection.prepareStatement(query);
            statement.setString(1, un);
            statement.setString(2, firstName);
            statement.setString(3, lastName);
            statement.setString(4, email);
            statement.setLong(5, mobile);
            statement.execute(query);
            System.out.println("Query 1 Executed...");
            
            response.sendRedirect("login.jsp");
        } catch (SQLException e) {
            out.println(e);
            e.printStackTrace();
        }

    }

}

Sigup.html

<%@include file="components/header.jsp" %>
<%@include file="components/navbar.jsp" %>
<div class="row">
    <div class="col-md-4"></div>
    <div class="col-md-4">
        <div class="card">
            <div class="card-header bg-danger">Create Account</div>
            <div class="card-body">

                <form action="Registration" method="get">
                <table class="table table-borderless">
                    <tr>
                        <td><input type="text" class="form-control" name="username"
                            placeholder="Enter Username" /></td>
                    </tr>
                    <tr>
                        <td><input type="password" class="form-control"
                            name="password" placeholder="Enter Password" /></td>
                    </tr>
                    <tr>
                        <td><input type="text" class="form-control" name="firstname"
                            placeholder="Enter Firstname" /></td>
                    </tr>
                    <tr>
                        <td><input type="text" class="form-control" name="lastname"
                            placeholder="Enter Lastname" /></td>
                    </tr>
                    <tr>
                        <td><input type="email" class="form-control" name="email"
                            placeholder="Enter Email" /></td>
                    </tr>
                    <tr>
                        <td><input type="tel" class="form-control" name="mobile"
                            placeholder="Mobile Number 10 digit" pattern="[0-9]{10}" /></td>
                    </tr>
                    <tr>
                        <td><input type="submit" class="btn btn-danger"
                            value="Create Account" /></td>
                    </tr>
                </table>
                </form>
            </div>
        </div>
    </div>
    <div class="col-md-4"></div>
</div>
<%@include file="components/footer.jsp" %>0

I tried query directly putting in mySql db in xamp here are my code it worked so query is correct and get method in jsp showing all parameter passing in url

user207421
  • 305,947
  • 44
  • 307
  • 483

1 Answers1

0

You need to create the Statement object before setting any paraameter on that, please uncomment the line

//statement = connection.prepareStatement("Your SQL Query here");

only after this you can use

statement.setString
Abdul Mohsin
  • 1,253
  • 1
  • 13
  • 24
  • i tride uncommenting it and with removing the null in following code Connection connection = null; PreparedStatement statement = null; and this error is showing up Cannot invoke "java.sql.Connection.prepareStatement(String)" because "this.connection" is null – Divyesh Kuchhadia Jul 20 '23 at 08:09