MES
MES

Reputation: 122

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.

Upvotes: 0

Views: 1749

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201527

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

Upvotes: 3

Related Questions