2014-05-13 58 views
0

我有一個簡單的程序,它使用jconnect6對一個Sybase ASE DB執行查詢。 程序迭代爲603所的記錄的結果集的while(rs.next())的NullPointerException

public ResultSet exec() 
{ 

     ResultSet rs = null; 
    try { 
     stmt= connection.createStatement(); 
     rs = stmt.executeQuery(query); 
    } catch (SQLException ex) { 
     ex.printStackTrace(); 
    } 
    try { 
     connection.close(); 
    } catch (SQLException ex) { 
     ex.printStackTrace(); 
    } 
     return rs; 
} 


public void Test() 
{ 
    ResultSet rs= exec(); 
    if(rs!=null) 
    { 
     int i=0; 
     try { 
      while(rs!=null && rs.next()) { // NullPointerException here 
       System.out.println(i++); 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

的輸出打印的值進行後拋出一個NullPointerException「I」直到其到達603而收集的記錄是超過1000和下面是誤差

May 13, 2014 11:43:43 AM appcomponents.OutageTest Test 
SEVERE: null 
java.lang.NullPointerException 
at com.sybase.jdbc3.timedio.RawDbio.reallyRead(Unknown Source) 
at com.sybase.jdbc3.timedio.Dbio.doRead(Unknown Source) 
at com.sybase.jdbc3.timedio.InStreamMgr.a(Unknown Source) 
at com.sybase.jdbc3.timedio.InStreamMgr.doRead(Unknown Source) 
at com.sybase.jdbc3.tds.TdsProtocolContext.getChunk(Unknown Source) 
at com.sybase.jdbc3.tds.PduInputFormatter.a(Unknown Source) 
at com.sybase.jdbc3.tds.PduInputFormatter.read(Unknown Source) 
at com.sybase.jdbc3.tds.TdsInputStream.read(Unknown Source) 
at com.sybase.jdbc3.tds.TdsInputStream.readInt(Unknown Source) 
at com.sybase.jdbc3.tds.TdsDataObject.readINTN(Unknown Source) 
at com.sybase.jdbc3.tds.TdsInt.beginRead(Unknown Source) 
at com.sybase.jdbc3.tds.TdsDataObject.doRead(Unknown Source) 
at com.sybase.jdbc3.tds.TdsInt.getLong(Unknown Source) 
at com.sybase.jdbc3.tds.CachedTdsInt.<init>(Unknown Source) 
at com.sybase.jdbc3.tds.TdsInt.createCachedCopy(Unknown Source) 
at com.sybase.jdbc3.tds.TdsResultSet.cacheCurrentRow(Unknown Source) 
at com.sybase.jdbc3.tds.TdsResultSet.next(Unknown Source) 
at com.sybase.jdbc3.jdbc.SybResultSet.next(Unknown Source) 
at appcomponents.OutageTest.Test(OutageTest.java:143) 
+3

的ResultSet不事到處傳遞。確保連接也不是空的。我會建議你將這個resultSet合併到'void Test'方法中。 – ErrorNotFoundException

+0

你知道在拋出npe時應該讀哪些數據嗎?你有這個hdbc驅動程序的源代碼嗎? – markusw

+0

@Stanley,你是對的,它接近那些603記錄是在我關閉連接之前收集到的,並且NPE由於這個錯誤而被拋出。 –

回答

1

我發現,在

rs = stmt.executeQuery(query); 

執行查詢後,我關閉連接,這是後運行的代碼修改:

public ResultSet exec() 
{ 

     ResultSet rs = null; 
    try { 
     stmt= connection.createStatement(); 
     rs = stmt.executeQuery(query); 
    } catch (SQLException ex) { 
     ex.printStackTrace(); 
    } 
//  try { 
//   connection.close();   //this is what was wrong with the code 
//  } catch (SQLException ex) { 
//   ex.printStackTrace(); 
//  } 
     return rs; 
} 
+1

正確。一旦你完成,你必須釋放連接,而不是之前。而且你不能忘記這樣做,否則你會擺脫可用的資源。 –

2

你應該,直到完成從ResultSet閱讀關閉連接。

public ResultSet exec() { 

    ResultSet rs = null; 
    try { 
     stmt = connection.createStatement(); 
     rs = stmt.executeQuery(query); 
    } catch (SQLException ex) { 
     ex.printStackTrace(); 
    } 
    return rs; 
} 

public void Test() { 
    ResultSet rs = exec(); 
    try { 
     if (rs != null) { 
      int i = 0; 
      try { 
       while (rs != null && rs.next()) { // NullPointerException here 
        System.out.println(i++); 
       } 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } catch (SQLException ex) { 
     ex.printStackTrace(); 
    } finally { 
     // ** Close your connection AFTER you've finished with the ResultSet 
     connection.close(); 
    } 
} 
0

你可以嘗試改變:

while(rs!=null && rs.next()) 

while(rs.next()) ?