2014-11-03 46 views
0

我的java代碼在存儲過程調用中將參數作爲系統當前日期時間和一小時時間傳遞給參數。如何修復java.lang.NullPointerException

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

當我運行相應的JSP頁面時,拋出了java.lang.NullPointerException

+0

顯示整個代碼。 – 2014-11-03 05:26:48

+0

日期d =新日期(); date currentDate = new Date(System.currentTimeMillis() - 3600 * 1000); public String [] [] getDbTable() {int i = 0; \t String [] [] a = new String [3600] [16]; clstmt = con.prepareCall(「exec vcs_gauge'vs1_bag','d','date'」); clstmt.setDate(「date」,new java.sql.Date(currentDate.getTime())); clstmt.execute(); rs = clstmt.getResultSet(); while(rs.next()) {for(int j = 0; j <16; j ++) { a [i] [j] \t = rs.getString(j + 1); } i ++; } finally {closeConnection(); } return a; } 這是我的JAVA CODE.SP有3個參數和最後兩個是日期類型。 – MES 2014-11-03 06:47:27

+0

@ Fumu7上面提到的是我的代碼。我知道它是以糟糕的方式呈現的,因爲我是新的堆棧流。 – MES 2014-11-03 06:49:08

回答

3

首先創建CallableStatement,然後綁定該值。這就是像換

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

您的語句的順序你得到了NullPointerException因爲clstmtnull直到你prepareCall()

編輯

我覺得你的語法應該是這樣

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

編輯2

根據您的更多詳細信息,

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

@Abdullah編輯我的問題 – MES 2014-11-03 06:30:19

+0

@EF當我做你提到的那麼然後參數日期沒有爲存儲過程定義vcs_gauge異常被拋出。 – MES 2014-11-03 06:31:42

+0

@MES編輯。我沒有注意到你的電話只是NPE。 – 2014-11-03 06:34:53