Reputation: 878
My question appears to be linked to the following question:
How to Handle in code IllegalStateException on Cursor?
The code for the offending method is as follows:
public boolean isPermanent(String screen_name) {
boolean output = false;
try {
Cursor c = mDb.query(USERS, new String[] {PERMANENT}, NAME + "='" + screen_name + "'", null, null, null, null);
if (c != null && c.getCount() > 0) {
c.moveToFirst();
output = c.getString(0).contentEquals("C");
c.close();
}
}
catch (Exception e) {
Log.e("DBERR", e.getMessage());
}
return output;
}
However, I am looking at the Cursor in question when the exception is thrown, I can see the following in Eclipse:
this SQLiteCursor (id=830061188288)
mClosed true
mColumnNameMap null
mColumns String[1] (id=830061188608)
mContentObservable ContentObservable (id=830061188456)
mContentResolver null
mCount 0
mCurrentRowID null
mCursorState 0
mDatabase SQLiteDatabase (id=830060407768)
mDataSetObservable DataSetObservable (id=830061188408)
mDriver SQLiteDirectCursorDriver (id=830061143904)
mEditTable "users" (id=830060403008)
mInitialRead 2147483647
mLock null
mMaxRead 2147483647
mNotificationHandler null
mNotifyUri null
mPendingData false
mPos -1
mQuery SQLiteQuery (id=830061143936)
mRowIdColumnIndex -1
mSelfObserver null
mSelfObserverLock Object (id=830061188504)
mSelfObserverRegistered false
mStackTraceElements null
mUpdatedRows HashMap (id=830061144056)
mWindow null
And this clearly shows that the state of the Cursor is closed, as it should be - so does anyone have any clues as to why this should be throwing an exception?
Upvotes: 1
Views: 1224
Reputation: 5107
Instead of checking c.getCount() > 0, check for c.moveToNext():
try{
...
if (c != null && c.moveToNext())
//do your thing here and don't call moveToFirst()
} finally {
if (c != null)
c.close();
}
And move the close to finally{}
Upvotes: 1