I have this html
form which is a date input type
<form method="POST" action="FillTanks" onsubmit="return(showAlert());">
<div class="container">
<div class="row">
<div class="col-4">
</div>
<div class="form-group md-form">
<i class="fa fa-calendar prefix grey-text"></i>
<input style="color: gray;" type="date" id="datedd" class="form-control">
</div>
<div class="text-center">
<button type="submit" id="btn-fill" class="btn btn-blue btn-lg"><strong>Fill</strong></button>
</div>
</div>
<div class="col-4">
</div>
</div>
</div>
</form>
And this is my servlet
public class FillTanks extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String fuelType = request.getParameter("fueltype");
String liters = request.getParameter("liters");
String date = request.getParameter("datedd");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Vehicle", "root", "");
PreparedStatement ps = con.prepareStatement("INSERT INTO fuel(fueltype,liters,date) VALUES(?,?,?)");
Date date2 = new SimpleDateFormat("MM/dd/yyyy").parse(date);
ps.setString(1, fuelType);
ps.setString(2, liters);
ps.setDate(3, (java.sql.Date) date2);
int i = ps.executeUpdate();
if (i > 0) {
System.out.println("Success");
} else {
System.out.println("not success");
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(FillTanks.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(FillTanks.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(FillTanks.class.getName()).log(Level.SEVERE, null, ex);
}
}
But everytime I execute the project, it gives me this error.
> Warning: StandardWrapperValve[FillTanks]: Servlet.service() for servlet FillTanks threw exception
java.lang.NullPointerException
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1439)
at java.text.DateFormat.parse(DateFormat.java:364)
at FillTanks.doPost(FillTanks.java:37)
I have used date
datatype when creating the database table related to this servlet.
Can anyone please help me what is wrong with my implementations?