Hello Users,

I have been learning the Prepared Statements in Java which is a newer and better way of entering details into the Database.

Have been getting problems with the insertion: NullPointerException and still don't know where I am going wrong.

Please Assist me in Solving the Issue.

Code Below:

Registration Method:

public void SignUser(String Username , String HashWord)

           throws ClassNotFoundException , SQLException , Exception , NullPointerException
    {

        String Password = "";

        MessageDigest MD5 = MessageDigest.getInstance("MD5");

        MD5.update(Password.getBytes());

        BigInteger Hash = new BigInteger(1 , MD5.digest());

        HashWord = Hash.toString(16);

        Connection SQLConnector = ConnectToTheDatabase();

        PreparedStatement PS = null;

        String RegistrationQuery = "INSERT INTO trial (Username , HashWord) VALUES ('" + Username + "' , '" + HashWord + "')";

        PS = SQLConnector.prepareStatement(RegistrationQuery);

        PS.executeUpdate();


    }

Registration Servlet

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package User;

import DataAccessObject.DAO;
import DataAccessObject.DAO_Interface;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author SaguWesker
 */
public class SignUp extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    response.setContentType("text/html;charset=UTF-8");

    PrintWriter out = response.getWriter();

    try 

    {

        RequestDispatcher rd = request.getRequestDispatcher("Relay");

        DAO_Interface Dao = DAO.getDAOInterface();

        Dao.SignUser(request.getParameter("Username"), request.getParameter("HashWord"));

        HttpSession Session = request.getSession();

        String Username = request.getParameter("Username");

        Session.setAttribute("Username", request.getParameter("Username"));

        RequestDispatcher sign_up_successful = request.getRequestDispatcher("index.jsp");

        sign_up_successful.forward(request, response);

    }


    catch (ClassNotFoundException CNFE)
    {
        System.out.println(CNFE);
    }

    catch (SQLException SQLE)
    {
        System.out.println(SQLE);
    }

    catch (Exception E)
    {
        E.printStackTrace();
    }

    }

    //finally 
    //{            

        //out.close();

    //}

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

and where are you getting a nullpointerexception? what line of your code, what variable, ... ?

Hy stultuske.

Want To Thank You For The Reply.

Unfortunately the error comes as follows:

java.lang.NullPointerException

The Line of Code and the Variable is not specified.

Is there a problem with the Prepared Statement.

Im passing all parameters from the JSP which is entering the Servlet.

Is the Prepared Statement Part Wrong?

Thank You Again

Marking Thread Solved. An Error in the JSP and Servlet Helped me Solve The Issue.

Thank You All For Your Support.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.