while时的NullPointerException(rs.next())

时间:2014-05-13 10:01:22

标签: java jdbc nullpointerexception resultset jconnect

我有一个简单的程序,它使用jconnect6在Sybase ASE DB中再次执行查询。 在迭代ResultSet的603条记录后,程序抛出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();
        }
    }
}

输出打印'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 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分: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;
}

答案 2 :(得分:0)

您可以尝试更改:

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

while(rs.next()) ?