title: IllegalStateException: Couldn't read row 0, col -1 from CursorWindow, despite checking for empty cursor?
body: <p>I have the following code: <code>result</code> is the cursor.</p>
  
  <pre><code>    if (result.getCount() == 1 &amp;&amp; result.getColumnCount() &gt; 0) {
          result.moveToFirst();
          int columnIndex = result.getColumnIndex(TrackerContract.WorkLog.COLUMN_BLOCK_IN);
          long time = currentTimeMilliMinutes();
          sumTotal = time - result.getLong(columnIndex);
      }
  </code></pre>
  
  <p>So, even though I'm checking for rows and columns, it throws the error in the title.  I don't know what else I should be doing to prevent this error.  The cursor should only have 1 row, 1 column, value <code>long</code>, I have checked the sql statement via <code>adb</code> and it seems to be correct.</p>
  
  <p>EDIT:</p>
  
  <p>Here is the query:</p>
  
  <pre><code>Cursor result = db.rawQuery("SELECT MAX("+TrackerContract.WorkLog.COLUMN_BLOCK_IN+
              ") FROM "+TrackerContract.WorkLog.TABLE_NAME+" WHERE "+TrackerContract.WorkLog.COLUMN_FDP_BEGIN+
              "=(SELECT MAX("+TrackerContract.WorkLog.COLUMN_FDP_BEGIN+") FROM "+TrackerContract.WorkLog.TABLE_NAME+");", 
              null);
  </code></pre>
  
  <p>When I enter the same sql via adb, I get the correct long value as the only return value (1 row, 1 col).  Here is the actual SQL statement, incase my encoding didn't go correctly.  I plan on refactoring this to use the parameterized query method rather than raw sql, but for now I am just making sure it works, and using the raw statements is easier for me.</p>
  
  <pre><code>select max(blockTimeEnd) from WorkLog where dutyDayStart=(select max(dutyDayStart) from WorkLog);
  </code></pre>
  
  <p>The Stack Trace:</p>
  
  <pre><code>12-25 16:55:00.477: E/AndroidRuntime(32708): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
  12-25 16:55:00.477: E/AndroidRuntime(32708):    at android.database.CursorWindow.nativeGetLong(Native Method)
  12-25 16:55:00.477: E/AndroidRuntime(32708):    at android.database.CursorWindow.getLong(CursorWindow.java:507)
  12-25 16:55:00.477: E/AndroidRuntime(32708):    at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75)
  12-25 16:55:00.477: E/AndroidRuntime(32708):    at com.berrmal.timetracker.MainActivity.currentRest(MainActivity.java:503)
  12-25 16:55:00.477: E/AndroidRuntime(32708):    at com.berrmal.timetracker.MainActivity.updateTimeTotals(MainActivity.java:567)
  12-25 16:55:00.477: E/AndroidRuntime(32708):    at com.berrmal.timetracker.MainActivity.populateViews(MainActivity.java:187)
  12-25 16:55:00.477: E/AndroidRuntime(32708):    at com.berrmal.timetracker.MainActivity.onResume(MainActivity.java:183)
  </code></pre>
  
  <p>Edit 2:
  did some logging/debugging, the column name in the cursor returned is <code>max(blockTimeEnd)</code> where it should be <code>blockTimeEnd</code>.  This is clearly the error.</p>