5

On Crashlytics, I see many Room exceptions with this message: "Make sure the Cursor is initialized correctly before accessing data from it". I cannot produce this error on my devices, and the error does not occur on a specific Android version or device model.

I cannot find anything about this error on Room. There are question for SQLLite and most of them are about problematic SQL queries which are written by hand.

Even this simple query has errors on customer devices.

@Query("SELECT * FROM UserAction WHERE UserAction.userId LIKE :userId " +
        " ORDER BY last_update_time DESC")
public List<UserAction> allList(String usedId);

The exception is:

Fatal Exception: java.lang.IllegalStateException: Couldn't read row 1756, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. at android.database.CursorWindow.nativeGetString(CursorWindow.java) at android.database.CursorWindow.getString(CursorWindow.java:438) at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:66)

The error occurs on generated code inside of while loop

  while(_cursor.moveToNext()) {
    final UserAction _item;
    _item = new UserAction();
    _item.userId = _cursor.getString(_cursorIndexOfUserId); //Exception here
fthdgn
  • 1,339
  • 1
  • 13
  • 18

1 Answers1

1

I am not sure I have the same issue. I get a crash in getAll (SELECT * FROM) function. My stack:

java.lang.IllegalStateException: Couldn't read row 83, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. at android.database.CursorWindow.nativeGetLong(Native Method) at android.database.CursorWindow.getLong(CursorWindow.java:507) at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:86) at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:220) at android.database.AbstractCursor.moveToNext(AbstractCursor.java:245) at Dao_Impl.getAll(LoggingDao_Impl.java:99) at java.lang.Thread.run(Thread.java:838)

Before this exception, I have a log:

W/CursorWindow: Window is full: requested allocation 2282898 bytes, free space 2086484 bytes, window size 2097152 bytes

My entity contains String column text. I inserter 2.2Mb buffer into it successfully. Unfortunately android cannot read it. The max size for CursorWindow is 2 Mb. Read more https://bugzilla.mozilla.org/show_bug.cgi?id=1280409.

I found two possible solutions for me:

  1. Ignore the row with big buffer when get all items: SELECT * FROM name WHERE length(text)<100000

  2. Select trimmed text: SELECT id,SUBSTR(text,0,100000) as text FROM name

vvkatwss vvkatwss
  • 3,345
  • 1
  • 21
  • 24
  • 1
    When the row is too large, the exception is "SQLiteBlobTooBigException" on my tests. Do your tests throw "IllegalStateException: Couldn't read row 83, col 0 from CursorWindow"? – fthdgn Oct 18 '19 at 11:54
  • 1
    Yes, I get IllegalStateException. My device 4.2.2, API 29 and String datatype. – vvkatwss vvkatwss Oct 21 '19 at 08:25