-1
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
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(name = "SimpleServlet", urlPatterns = { "/SimpleServlet" })
public class SimpleServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

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

        String username = request.getParameter("username").toString();
        String password = request.getParameter("password").toString();
        String repassword = request.getParameter("repassword").toString();
        String ph = request.getParameter("phone").toString();
        double phone = Double.parseDouble(ph);

        String address = request.getParameter("address").toString();

        int status = register(username, password, repassword, phone, address);

        if (status > 0) {

            RequestDispatcher rd = request.getRequestDispatcher("Register_success.jsp");
            rd.include(request, response);
        } else {
            RequestDispatcher rd = request.getRequestDispatcher("Register_fail.jsp");
            rd.include(request, response);
        }

        out.close();
    }

    static int status = 0;

    @SuppressWarnings("null")
    public static int register(String username, String password, String repassword, double phone, String address) {

        PreparedStatement ps;
        Connection con;
        con = GetCon.getCon();
        try {
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT username FROM newuser where username='" + username + "'");

            while(rs.next())
            if (rs.getString("username").equalsIgnoreCase(username)) {
                System.out.println(username);
                HttpServletResponse response = null;
                response.sendRedirect("UserAlreadyExist.jsp");
            } else {

                ps = con.prepareStatement("Insert into newuser values(?,?,?,?,?)");
                ps.setString(1, username);
                ps.setString(2, password);
                ps.setString(3, repassword);
                ps.setDouble(4, phone);
                ps.setString(5, address);

                status = ps.executeUpdate();
            }
        } catch (SQLException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
        return status;

    }
}

Hello, I have a problem with executing above code. When we enter existing username it throw an NullPointerException. Please someone help me to find error. The code work fine with new username but not with existing. Thank You !

  • Read the stack trace and use a debugger. YOu should be able to locate the exact line where it throws an exception and then fix your issue – jhamon Jan 04 '16 at 09:36
  • can you point the line where you are facing the issue. – Vivek Singh Jan 04 '16 at 09:36
  • 1
    HINT: `HttpServletResponse response = null; response.sendRedirect("UserAlreadyExist.jsp");` response is null – jhamon Jan 04 '16 at 09:37

1 Answers1

1

The response is null in your register method

HttpServletResponse response = null;   
response.sendRedirect("UserAlreadyExist.jsp");

Use the responseobject from doPost(HttpServletRequest request, HttpServletResponse response) in order to redirect the user.

You will need to add an argument to the register method.

Your IDE shows warnings to help you. Don't use @SuppressWarnings("null")

jhamon
  • 3,346
  • 3
  • 23
  • 35