简体   繁体   中英

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.

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.

First create the CallableStatement and then bind the value. 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() .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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