gpt4 book ai didi

servlet 中的 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-11-30 11:21:10 24 4
gpt4 key购买 nike

<分区>

当我运行我的 servlet 时,我在我的 Tomcat 控制台中收到此错误。我想我必须看看我的 SimpleDateformat,但是有人能看出这里出了什么问题吗?我的代码编译正常,所以我有点怀疑要注意什么:

错误视频: https://www.youtube.com/watch?v=3hdmIkwuB6k&feature=youtu.be

祝大家度过愉快的一天/晚上来自朱莉

我的小服务程序:

package WorkPackage;

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 startdate = ? AND endDate = ? ";
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 );

//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);
}
}
}
}

Javascript:

    <form id="myForm">
<input type="text" id="startDate"/>
<input type="text" id="endDate"/>
</form>
<div id="startresult"></div>
<div id="endresult"></div>
<script>

$(function(){
$("#startDate").datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function(dateText,inst){
$('.selected-date').html(dateText);
var JSON = $('#myForm').serializeArray(); //you forgot to create JSON data
$.ajax({
url: "../getHoursSQL",
type: "post",
data: JSON,
success: function(data){
start: $("#startDate").val();
alert("success");
$("#startresult").html(data);

},
error:function(){
alert("failure");
$("#startresult").html('there is error while submit');
}
});
}
});
});

$(function(){
$("#endDate").datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function(dateText,inst){
$('.selected-date').html(dateText);
var JSON = $('#myForm').serializeArray(); //you forgot to create JSON data
$.ajax({
url: "../getHoursSQL",
type: "post",
data: JSON,
success: function(data){
start: $("#endDate").val();
alert("success");
$("#startresult").html(data);

},
error:function(){
alert("failure");
$("#startresult").html('there is error while submit');
}
});
}
});
});

</script>

带有新 javascript 代码的新控制台错误:

java.lang.NullPointerException
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1234)
at java.text.DateFormat.parse(DateFormat.java:335)
at WorkPackage.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)

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com