简体   繁体   English

如何修复java.lang.NullPointerException

[英]How to fix java.lang.NullPointerException

My java code passes parameters as system current date-time and one hour before time in stored procedure call. 我的Java代码在存储过程调用中将参数作为系统当前日期时间和时间之前一小时传递。

Date d = new Date();
Date currentDate = new Date(System.currentTimeMillis() - 3600 * 1000); 
clstmt.setDate("date", new java.sql.Date(currentDate.getTime())); 
clstmt = con.prepareCall("exec vcs_gauge 'vs1_bag', 'd', 'date'"); 

When I run the corresponding JSP page then the java.lang.NullPointerException is thrown. 当我运行相应的JSP页面时,将引发java.lang.NullPointerException

First create the CallableStatement and then bind the value. 首先创建CallableStatement ,然后绑定值。 That is swap the order of your statements like 那就是交换您的语句顺序,例如

// clstmt.setDate("date", new java.sql.Date(currentDate.getTime())); 
clstmt = con.prepareCall("exec vcs_gauge 'vs1_bag', 'd', 'date'"); 
clstmt.setDate("date", new java.sql.Date(currentDate.getTime()));

You get a NullPointerException because clstmt is null until you prepareCall() . 之所以会收到NullPointerException ,是因为clstmtnull直到您prepareCall()为止。

Edit 编辑

I think your syntax should be something like 我认为您的语法应该像

clstmt = con.prepareCall("{call vcs_gauge('vs1_bag', 'd', ?) }"); 
clstmt.setDate(1, new java.sql.Date(currentDate.getTime()));

Edit 2 编辑2

Based on your additional details, 根据您的其他详细信息,

String sql = "exec vcs_gauge @gauge_name=?,@first_rec_time=?,@last_rec_time=?";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
clstmt = con.prepareCall(sql); 
clstmt.setString(1, "vs1_bag");
clstmt.setString(2, df.format(d));
clstmt.setString(3, df.format(currentDate));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM