diff --git a/src/main/java/sqlline/BufferedRows.java b/src/main/java/sqlline/BufferedRows.java
index <HASH>..<HASH> 100644
--- a/src/main/java/sqlline/BufferedRows.java
+++ b/src/main/java/sqlline/BufferedRows.java
@@ -102,7 +102,10 @@ class BufferedRows extends Rows {
       // Add a row of column names as the first row of the first batch.
       list.add(columnNames);
     }
-    if (limit > 0) {
+    if (rs.isClosed()) {
+      // Result set is closed. Perhaps the driver closed it automatically
+      // because we reached the end. Do nothing.
+    } else if (limit > 0) {
       // Obey the limit if the limit is non-negative and this is the first
       // batch.
       int counter = 0;
[SQLLINE-<I>] DB2 JDBC driver throws if SQLLine attempts to call ResultSet.next after last row

Based on PR #<I> by Matt Innes, with additional code by Julian Hyde.

Hoping that it also fixes
[SQLLINE-<I>] sqlite3 java.lang.IllegalStateException: SQLite JDBC: inconsistent internal state