运行servlet的NullPointerException

时间:2014-03-14 08:57:25

标签: servlets nullpointerexception

)当我运行我的servlet时,我收到以下错误:有人能看到这是什么意思吗?我试图从表单中获取startDate和endDate。我想这是关于我的simpleDateFormat,但我看不出我做错了什么?

java.lang.NullPointerException
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1234)
    at java.text.DateFormat.parse(DateFormat.java:335)
    at getHoursSQL.doPost(getHoursSQL.java:40)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:695)

我的servlet看起来像这样:

import java.io.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet("/getHoursSQL")
public class getHoursSQL extends HttpServlet{

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException{

        String connectionURL = "jdbc:mysql://localhost/NekiWork";
        Connection connection=null;

        try {
            //Load database driver
            Class.forName("com.mysql.jdbc.Driver");
            //Connection to the database
            connection = DriverManager.getConnection(connectionURL, "root", ""); 
            //Getting the data from database

            String sql = "SELECT *, (Day_hours + (Day_minutes / 60)) AS Allday_hours FROM Workdata "
                    + " WHERE Date = ? AND Date = ? ";
            PreparedStatement pst = connection.prepareStatement(sql);

            SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");

            java.util.Date util_StartDate = format.parse( req.getParameter("startDate") );  
            java.sql.Date sql_StartDate = new java.sql.Date( util_StartDate.getTime() );

            java.util.Date util_EndDate = format.parse( req.getParameter("endDate") );      
            java.sql.Date sql_EndDate = new java.sql.Date( util_EndDate.getTime() );
                pst.setDate( 1, sql_StartDate );
                pst.setDate(2, sql_EndDate );
                System.out.println(req.getParameter("startDate"));                          
                System.out.println(req.getParameter("endDate"));
            //Show the result from database
                ResultSet rs = pst.executeQuery();

            float Allday_hours_sum = 0;
                while (rs.next()){                                      
                    Allday_hours_sum += rs.getFloat("Allday_hours"); 

                }   
                res.setContentType("text/html;charset=UTF-8");          
                res.getWriter().print(Allday_hours_sum); 

                pst.close();

        }
        catch(ClassNotFoundException e){

            System.out.println("Couldn't load database driver: " + e.getMessage());
        }
        catch(SQLException e){
            System.out.println("SQLException caught: " + e.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
        }
        finally {

            try {
                if (connection != null) connection.close();
            }
            catch (SQLException ignored){
                System.out.println(ignored);
            }
        }
    }
}

编辑代码:

PreparedStatement pst = connection.prepareStatement(sql);

            SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");

            java.util.Date util_startDate = format.parse( req.getParameter("startDate") );  
            java.sql.Date sql_startDate = new java.sql.Date( util_startDate.getTime() );

            java.util.Date util_endDate = format.parse( req.getParameter("endDate") );      
            java.sql.Date sql_endDate = new java.sql.Date( util_endDate.getTime() );

                pst.setDate( 1, sql_startDate );
                pst.setDate(2, sql_endDate );
                System.out.println(req.getParameter("startDate"));                          
                System.out.println(req.getParameter("endDate"));

1 个答案:

答案 0 :(得分:0)

你是如何在servlet页面中提到像pst.setDate(1, sql_EndDate );pst.setDate(2, sql_EndDate );这两个同名的。 我问题就在这里,你这样做:

  pst.setDate( 1, sql_StartDate );
  pst.setDate(2, sql_EndDate );

你应该使用像servlet这样的类型:

user.setDob(dob);

用户正在跟踪dao中的插入部分:

public void addUser(User user)
    preparedStatement.setDate(3, new java.sql.Date(user.getDob().getTime()));

同一代码

 SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd");
    Date startDate = fm.parse(req.getParameter("startDate"); 
    pst.setSDate(1,startDate);

 SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd");
    Date startDate = fm.parse(req.getParameter("endDate"); 
    pst.setEDate(2,startDate);