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?