0

I have JDBC Class and Servlet class separately in 2 packages. I try to fetch data from an HTML form and insert it into a table. This is the Servlet class code to fetch data

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

    String userName = request.getParameter("UserName");
    String password = request.getParameter("Password");
    String country = request.getParameter("Country");
    String address = request.getParameter("Address");
    String gender = request.getParameter("Gender");
    String years = request.getParameter("Years");
    String bDate = request.getParameter("BirthDate");
    Boolean hasCar = null; 
    if (request.getParameter("Car")!= null){
        hasCar = true;
    }else{
        hasCar = false;
        }

and this is the method in JDBC class to insert data

public static void insertData(User user){


        String sql = "insert into users values (1,?,?,?,?,?,?,?,?)";
        PreparedStatement ps = null;
        String userName = user.getUserName();
        String password = user.getPassword();
        String address = user.getAddress();
        String country = user.getCountry();
        String gender = user.getGender();
        int years = Integer.parseInt(user.getYears());
        String bbd = user.getBDate();
        Date bDate = null;
    try {
        bDate = (Date) new SimpleDateFormat("dd/MM/yyyy").parse(bbd);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    boolean hasCar = user.isHasCar();
        try {
            ps.setString(1, userName);
            ps.setString(2, password);
            ps.setString(3, country);
            ps.setString(4, address);
            ps.setString(5, gender);
            ps.setInt(6, years);
            ps.setDate(7, bDate);
            ps.setBoolean(8, hasCar);


            ps = conn.prepareStatement(sql);
            ps.executeUpdate(sql);

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

I created an object from class user in Servlet class and passed it to JDBC method and redirect into another html page

    User user = new User(userName, password, country, address, gender, years, bDate, hasCar);
    JDBCClass.insertData(user);
response.sendRedirect("Home.html");

but it throws java.lang.nullpointerexception

What is wrong?

Error photo

A. Maher
  • 1
  • 2
  • 4

2 Answers2

0

In your code you define PreparedStatement ps = null; and invoke ps.setString()first and then initial it,that's the reason cause NullPointerException,you need to init it before using it

ps = conn.prepareStatement(sql);//need to init it now
ps.setString(1, userName);
ps.setString(2, password);
ps.setString(3, country);
ps.setString(4, address);
ps.setString(5, gender);
ps.setInt(6, years);
ps.setDate(7, bDate);
ps.setBoolean(8, hasCar);


//ps = conn.prepareStatement(sql);//this is the wrong place
ps.executeUpdate(sql);

Also,it's a very bad practice to omit column name when insert data

 String sql = "insert into users values (1,?,?,?,?,?,?,?,?)"; //it's bad,since you omit the column name
 String sql = "insert into users (col1,col2,col3,col4,col5,col6,col7,col8,col9)
   values (1,?,?,?,?,?,?,?,?)";
flyingfox
  • 13,414
  • 3
  • 24
  • 39
0

To answer your question :

You declare PreparedStatement ps = null; and then you try to execute method ps.setString(int,String) while ps is still null.

Prepared statements should be "prepared" before you assign the form values to their proper position indexes.

If you move the ps = conn.prepareStatement(sql); before the value assignation it should work. Like so:

boolean hasCar = user.isHasCar();
    try {
        ps = conn.prepareStatement(sql);
        ps.setString(1, userName);
        ps.setString(2, password);
        ps.setString(3, country);
        ps.setString(4, address);
        ps.setString(5, gender);
        ps.setInt(6, years);
        ps.setDate(7, bDate);
        ps.setBoolean(8, hasCar);

        ps.executeUpdate(sql);

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

Comment:

I would like to note that assigning variables to null makes your code more prone to those types of errors that we observe here.