Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:34:58+00:00 2026-06-02T06:34:58+00:00

I am trying to use java servlet to input data into the database. package

  • 0

I am trying to use java servlet to input data into the database.

package new;

import java.io.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class display extends HttpServlet{
  public void init(ServletConfig config) throws ServletException{
  super.init(config);
  }


  protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        System.out.println("command: " + request.getParameter("command"));
        response.setContentType("text/html");
        java.io.PrintWriter out = response.getWriter();
            out.println("<title>Reg</title></head><body>");
        out.println("<h2>REG Form</h2><tr>");

        // HTMl Customer Input
        out.println("<form method=\"post\" action =\""
                + request.getContextPath() + "/display\" >");
        out.println("<table width=\"440\" border=\"0\">");
        out.println("<tr><th width=\"124\" scope=\"row\">Artist Info</th>");
        out.println("<td width=\"150\">Details</td>");
        // First input box: DOB
        out.println("<tr><th scope=\"row\">DOB:</th>");
        out.println("<td><input type=\"text\" name=\"dob\" size=\"20\" ></td>");
        out.println("</tr>");
        // Second input box: First Name
        out.println("<tr><th scope=\"row\">Firts name:</th>");
        out.println("<td><input type=\"text\" name=\"firtsname\" size=\"20\" ></td>");
        out.println("</tr>");
        // Third input box: Last name
        out.println("<tr><th scope=\"row\">Last name:</th>");
        out.println("<td><input type=\"text\" name=\"lastname\" size=\"20\" ></td>");
        out.println("</tr>");
        // Submit button
        out.println("</td></tr><tr><td valign=\"top\">");
        out.println("<input type=\"submit\" value=\"Register\"></td></tr>");
        out.println("</table></form>");
        out.println("</body></html>");
    }



  /**Process the HTTP Get request*/


  protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // Establish variables

        // HTML results
        response.setContentType("text/html");
        java.io.PrintWriter out = response.getWriter();
            out.println("<title>Result</title></head><body>");
        out.println("<h2>Welcome.</h2></br>");


        //Establish Variables for percentages of user input for each state and for each party
        int birthyear=0;
        String familyname = null, givenname = null;

        //DOB
        if (request.getParameter("birthyear").equals(""))   {
            out.println("Input date of birth");
        //otherwise, get the input
    }   else    {
        birthyear = Integer.parseInt(request.getParameter("birthyear"));
    }

        if (request.getParameter("familyname").equals(""))  {
            out.println("Input first name");

    }   else    {
        familyname = request.getParameter("familyname");
    }

        if (request.getParameter("givenname").equals(""))   {
            out.println("Input last name");

    }   else    {
        givenname = request.getParameter("givenname");
    }


  Connection connection=null;

  int id = 0;
  int agentid = id++;

  try {
  // Load the database driver
        Class.forName("org.postgresql.Driver");
        connection = DriverManager.getConnection(
                "jdbc:postgresql://localhost:5432/caglar", "postgres",
                "6666yyy");
   //Add the data into the database
  String sql = "insert into agent values (?,?,?,?)";
  PreparedStatement pst = 
  connection.prepareStatement(sql);
  pst.setInt(1, agentid);
  pst.setInt(2, birthyear);
  pst.setString(3, familyname);
  pst.setString(4, givenname);

   }
  catch(ClassNotFoundException e){
  out.println("Couldn't load database driver: " 
  + e.getMessage());
  }
  catch(SQLException e){
  out.println("SQLException caught: " 
  + e.getMessage());
  }
  catch (Exception e){
  out.println(e);
  }
  finally {
  // Always close the database connection.
  try {
  if (connection != null) connection.close();
  }
  catch (SQLException ignored){
  out.println(ignored);
  }
  }
  }
}

I get the following error in doPost:

java.lang.NullPointerException
    S517.display.doPost(display.java:73)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Does anybody know why?? I simply want to add data into the db using a servlet. Driver exist in the library.

line 73 & 74

    if (request.getParameter("birthyear").equals(""))   {
        out.println("Input date of birth");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:35:01+00:00Added an answer on June 2, 2026 at 6:35 am

    Your error is on line 73:

    birthyear = Integer.parseInt(request.getParameter("birthyear"));
    

    I’m guessing by your code that the form in the doGet function is the form posting the data you want to store in your database. Your form doesn’t contain a birthyear field. Its absence makes request.getParameter(“birthyear”) return null, which causes a NullPointerException when trying to parse an int from it.

    Furthermore, you should not only prepare your statement, but execute it as well:

    PreparedStatement pst = connection.prepareStatement(sql);
    pst.setInt(1, agentid);
    pst.setInt(2, birthyear);
    pst.setString(3, familyname);
    pst.setString(4, givenname);
    
    // add this line
    pst.executeUpdate();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having NullPointerException trying to getCurrentSession() java.lang.NullPointerException servlets.ControlServlet.doPost(ControlServlet.java:46) javax.servlet.http.HttpServlet.service(HttpServlet.java:709) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) I use Tomcat 5.5
I'm new to Java server programming, and I'm trying to use Google app engine.
I'm trying to use a Json View for Spring ( http://spring-json.sourceforge.net/ ) (org.springframework.web.servlet.view.json.JsonView) but
I am trying to use Java's useDelimiter method on it's Scanner class to do
I am trying use a Java Uploader in a ROR app (for its ease
I'm really stuck trying to use java wrapper library for opencv's cvMatchTemplate. See this
I've been trying to use the Java SAX parser to parse an XML file
I am a complete JSP beginner. I am trying to use a java.util.List in
I'm trying to re-use a Java generic collection I wrote, looks a lot like
I'm trying to find the best way to use Java libraries that were installed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.