0

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)
Amr Nassar
  • 155
  • 2
  • 9
  • 3
    ResultSets are not things to be passed around. Make sure that the Connection is also not Null. I would Suggest you Incorporate that resultSet into the `void Test` Method. – Stanley Mungai May 13 '14 at 10:04
  • do you know which data should be read when the npe is thrown? Do you have the source code of that hdbc driver? – markusw May 13 '14 at 10:04
  • @Stanley, You are right, It seams that those 603 records are what have been collected just before I close the connection and the NPE is thrown because of this mistake. – Amr Nassar May 13 '14 at 10:10

3 Answers3

2

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();
    }
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
1

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;
}
Amr Nassar
  • 155
  • 2
  • 9
  • 1
    Correct. You must release connection once you are done and not before. And you must not forget to do it otherwise you will get out of available resources. – Leos Literak May 13 '14 at 10:09
0

You can try to change :

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

To

while(rs.next()) ?
coldy
  • 2,115
  • 2
  • 17
  • 28