Reputation: 155
I have a simple program that executes a query agains a Sybase ASE DB using jconnect6 . the program throws a NullPointerException after iterating for 603 records of the ResultSet
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();
}
}
}
The output prints the value for 'i' till it reaches 603 while the collected records are more than 1000 and below is the error
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)
Upvotes: 0
Views: 5029
Reputation: 2195
You can try to change :
while(rs!=null && rs.next())
To
while(rs.next()) ?
Upvotes: 0
Reputation: 65821
You should not close your connection until you have finished reading from the 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();
}
}
Upvotes: 2
Reputation: 155
I have found that after executing the query at
rs = stmt.executeQuery(query);
I close the connection and this is the code that runs after the modification:
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;
}
Upvotes: 1