I have the following code: result
is the cursor.
if (result.getCount() == 1 && result.getColumnCount() > 0) {
result.moveToFirst();
int columnIndex = result.getColumnIndex(TrackerContract.WorkLog.COLUMN_BLOCK_IN);
long time = currentTimeMilliMinutes();
sumTotal = time - result.getLong(columnIndex);
}
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 long
, I have checked the sql statement via adb
and it seems to be correct.
EDIT:
Here is the query:
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);
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.
select max(blockTimeEnd) from WorkLog where dutyDayStart=(select max(dutyDayStart) from WorkLog);
The Stack Trace:
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)
Edit 2:
did some logging/debugging, the column name in the cursor returned is max(blockTimeEnd)
where it should be blockTimeEnd
. This is clearly the error.