aspose file tools*
The moose likes JSP and the fly likes Null pointer in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Null pointer in JSP" Watch "Null pointer in JSP" New topic
Author

Null pointer in JSP

suraj mallik
Greenhorn

Joined: Jul 20, 2013
Posts: 1
<%@page import="java.sql.*"%>
<% java.util.Locale locale = request.getLocale(); %>
<%@page import="java.util.Properties"%>
<%@page import="java.text.*" %>>
<%@page import="java.util.Date"%>

<html>
<body>
<form>
Name:<input type="text" name="realname">
<br>
e-mail:<input type="email" name="email">
<br>
comments:<input type="text" name="comments">
<br>
Date:<input type="number" name="date">
<input type="submit">
</form>
<%


try


{
String name = request.getParameter("realname");
String email = request.getParameter("email");
String comments = request.getParameter("comments");
String date1= request.getParameter("date");

java.util.Date date =new SimpleDateFormat("yyyy-mm-dd").parse(request.getParameter("date"));
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
Class.forName("com.ibm.db2.jcc.DB2Driver");
Connection con= DriverManager.getConnection("jdbc:db2://localhost:50000/sj","sjk","klklkl");
PreparedStatement pst=con.prepareStatement("insert into guest_feedback (name,email,comments,feedback_date) values( ?,?,?,? )");

pst.setString(1,name);

pst.setString(2,email);
pst.setString(3,comments);
pst.setDate(4,sqlDate);

int stat= pst.executeUpdate();
//PrintWriter out = response.getWriter();
if (stat>0 ) // checking whether the query was executed.
{

System.out.println("Record Inserted");
}
else

System.out.println("Record Not Inserted");
pst.close();
con.close();

}
catch (SQLException e)
{
System.out.println("Query is not executed" + e.getMessage());
}
catch (ClassNotFoundException ce)
{
System.out.println("Class Not Found " + ce.getMessage());
//e.printStackTrace();
}

%>
</body>
</html>


I think there is some problem with date.please help me in solving i am a newbie in jsp.Upon executin it shows the following error:
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /test1.jsp at line 31

28: String comments = request.getParameter("comments");
29: //String date1= request.getParameter("date");
30: //String d = "2006-11-10";
31: java.util.Date date =new SimpleDateFormat("yyyy-mm-dd").parse(request.getParameter("date"));
32: java.sql.Date sqlDate = new java.sql.Date(date.getTime());
33: Class.forName("com.ibm.db2.jcc.DB2Driver");
34: Connection con= DriverManager.getConnection("jdbc:db2://localhost:50000/suraj","kiit","surajmallik");


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


root cause

java.lang.NullPointerException
java.text.SimpleDateFormat.parse(Unknown Source)
java.text.DateFormat.parse(Unknown Source)
org.apache.jsp.test1_jsp._jspService(test1_jsp.java:89)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Step 1: move the Java code out of the JSP and into a servlet that the form will submit to. Modern JSPs should never have Java code in them, and here, you're trying to process the form before it even gets a chance to be seen at the browser.

Step 2: Always check parameters for null. Nothing guarantees that they'll have a value.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

If you are a novice to JSP, I always recommend reading
  • The Secret Life of JSPs
  • The Front Man

  •  
     
    subject: Null pointer in JSP