[JAVA] [SQLite] IllegalStateException that occurs in Cursor

We received the following inquiries from users of the app KUMIWAKE that I developed.

I used to use it normally, but when I try to select or group members, I get "Kumiwake will be closed due to a problem". What should i do?

For some reason, there were many crashes in the latest version, so these opinions are really helpful.

Cause of error

However, there is one problem here. I can't reproduce the error in my environment. Neither the actual machine nor the AVD.

Looking at the stack trace in the Play Console, it was as follows.

java.lang.IllegalStateException: 
  at android.database.CursorWindow.nativeGetString (CursorWindow.java)
  at android.database.CursorWindow.getString (CursorWindow.java:438)
  at android.database.AbstractWindowedCursor.getString (AbstractWindowedCursor.java:51)
  ...

When I looked it up, it looked like an error due to a database cursor.

It seems that it worked before, so I reviewed the change history of the Cursor part and managed to identify the cause.

c.getString (c.getColumnIndex (COL_NAME)) ← This part

https://codeday.me/jp/qa/20190421/668862.html It seems that getColumnIndex (COL_NAME) could not find the Index of COL_NAME and returned -1 as in this answer. This results in c.getString (-1) and the column cannot be found! You said an error. (Column number is 0 ~)

solution

For the time being, I settled down in the direction of not using getColumnIndex (). (I really want to use it ...)

Before modification: c.getString (c.getColumnIndex (COL_NAME)) After modification: c.getString (1)

I specified the column number directly. It seems that it worked normally after the correction. I'm happy.

If you want to use getColumnIndex (COL_NAME), -1 will be returned when the column is not found, so it is better to usegetColumnIndexOrThrow (COL_NAME)as in [this article] [1]. That's right.

Questions remain

https://teratail.com/questions/128793 Someone was suffering from a similar problem. As you can see in this person's question, it seems that getColumnIndex () may or may not return a good value depending on the terminal. It didn't depend on the Android version, so maybe it's a device issue ... If you are familiar with SQLite, please teach me. (Crying)

reference

Android Developers [Yuki's branch Android: Cursor API summary] [1] [1]:http://yuki312.blogspot.com/2012/03/androidcursorapi.html

Recommended Posts

[SQLite] IllegalStateException that occurs in Cursor
What to do when IllegalStateException occurs in PlayFramework
[Java] Explains ConcurrentModificationException that occurs in java.util.ArrayList for newcomers
IllegalArgumentException occurs in PathMatcher :: matches