title: Android Reading cursor without rows
body: <p>I wrote extension functions for reading from cursor:</p>
  
  <pre><code>fun SQLiteDatabase.query(table: String,
                           columns: Array&lt;String&gt;,
                           selection: String? = null,
                           selectionArgs: Array&lt;String&gt;? = null): Cursor =
          query(table, columns, selection, selectionArgs, null, null, null)
  
  fun Cursor.readRows(onRowRead: (Cursor) -&gt; Unit) {
      if(moveToFirst()) {
          do {
              onRowRead(this)
          } while (moveToNext())
      }
  }
  
  fun Cursor.getString(field: String): String = getString(getColumnIndex(field))
  fun Cursor.getInt(field: String) = getInt(getColumnIndex(field))
  </code></pre>
  
  <p>And it mostly works perfect. But recently happened strange error in the followng <code>SQLiteOpenHelper</code> function:</p>
  
  <pre><code>fun loadPresentationStatistic(stringParser: (String) -&gt; PresentationStatistic): ArrayList&lt;PresentationStatisticInformation&gt; {
      val db = readableDatabase
      val c = db.query(TABLE_PRESENTATION_STATISTIC, arrayOf(ROW_ID, ROW_PRESENTATION_STATISTIC))
      val result = arrayListOf&lt;PresentationStatisticInformation&gt;()
      c.readRows {
          result.add(PresentationStatisticInformation(it.getInt(ROW_ID),stringParser(it.getString(ROW_PRESENTATION_STATISTIC))))
      }
      c.close()
      return result
  }
  </code></pre>
  
  <p>Error stacktrace:</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: Couldn't read row 0, col 0 from
    CursorWindow.  Make sure the Cursor is initialized correctly before
    accessing data from it.
    android.database.CursorWindow.nativeGetLong(Native Method)
    android.database.CursorWindow.getLong(CursorWindow.java:511)
    android.database.CursorWindow.getInt(CursorWindow.java:578)
    android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84)
    com.example.DBWorkerKt.getInt(DBWorker.kt:480)
    com.example.DBHelper$loadPresentationStatistic$1.invoke(DBWorker.kt:342)</p>
  </blockquote>
  
  <p>If I get it right, that means <code>moveToFirst</code> returns true for empty cursor, so <code>getInt</code> function throws this exception. How could that be?</p>
  
  <p>The worst thing is that I can't reproduce it. It happens only on one particular device - all other users works as they should.</p>