0

i have posted my coding below, i want to enter the date and store it into the database throught j s p. when i execute i get null pointer exception. what change must be done? can anyone help?

my stack trace is

Stacktrace:

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

java.lang.NullPointerException
java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1235)
java.text.DateFormat.parse(DateFormat.java:335)
org.apache.jsp.sample_005fvalidate_jsp._jspService(sample_005fvalidate_jsp.java:101)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

Code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"      "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>ALLOCATION</title>
</head>

<body bgcolor="#FFFFFF" text="#000000">


<%
    Connection con = null;

    String StaffName = request.getParameter("StaffName");
    String hourId = request.getParameter("hourId");
    if(hourId==null)
    hourId="";
    String day = request.getParameter("day");
   if (day==null)
    day="";
    String date = request.getParameter("date");
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");here
  java.util.Date dateStr = formatter.parse(date); // (this is the line that cannot be proccessed)
   java.sql.Date dateDB = new java.sql.Date(dateStr.getTime());
   if (date==null)
      date="";

    try {
          Class.forName("com.mysql.jdbc.Driver");
          con =  DriverManager.getConnection("jdbc:mysql://localhost:3306/StaffAllocation","root","success") ;

       // PreparedStatement stat = con.PrepareStatement();
        String updateString ="INSERT INTO tblstaffallocation     (StaffName,hourId,daysId,date) VALUES (?,?,?,?)";   
       //'"+StaffName+"','"+hour+"','"+day+"','"+date+"')
        PreparedStatement preparedStatement = con.prepareStatement(updateString);


        preparedStatement.setString(1, StaffName);
       preparedStatement.setInt(2, 0);
       preparedStatement.setInt(3, 0);
       preparedStatement.setDate(4,dateDB);
        preparedStatement.executeUpdate();
4
  • date must be null in request. Try debugging and find out the value Commented Nov 20, 2013 at 8:26
  • @ gv an @ nos what change should i do? Commented Nov 20, 2013 at 8:29
  • How are you sending the parameter "date" to the jsp you've posted in your question? Could you please provide that code? Commented Nov 20, 2013 at 8:33
  • 1
    @user2951465 make sure you have given "date" name at the front end HTML code... Commented Nov 20, 2013 at 8:35

3 Answers 3

1
String date = request.getParameter("date");

The above line has null value. There is no "date" parameter in your request. Please check out. Check out the case of the parameter "date" in your request

Sign up to request clarification or add additional context in comments.

Comments

0

Make sure that the date is passed in request. Or else put a null check before the parse statement

java.util.Date dateStr;
java.sql.Date dateDB;

if(date!=null){
   dateStr = formatter.parse(date); 
   dateDB = new java.sql.Date(dateStr.getTime());
}

Comments

0

Try

     java.util.Date dateStr=null;
     java.sql.Date dateDB=null;
     if (date!=null){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        dateStr = formatter.parse(date);
        dateDB = new java.sql.Date(dateStr.getTime());
      }else{
          date="";
      }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.