为什么我得到空指针异常?

时间:2013-11-20 08:24:12

标签: java mysql jsp

我已在下面发布了我的编码,我想输入日期并通过j s p将其存储到数据库中。当我执行我得到空指针异常。必须做什么改变?有人可以帮忙吗?

我的堆栈跟踪是

堆栈跟踪:

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)

根本原因

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)

代码:

<%@ 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();

3 个答案:

答案 0 :(得分:1)

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

上面的行有空值。您的请求中没有“日期”参数。请检查。查看请求中参数“date”的大小写

答案 1 :(得分:0)

确保在请求中传递日期。或者在解析语句之前进行空检查

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

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

答案 2 :(得分:0)

尝试

     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="";
      }