Monday, November 2, 2009

[android-developers] SQLite, Cursors, and CursorIndexOutOfBoundsException

I'm writing myself a flashcard program that uses a database. I used
the notepad example as a template and modified it accordingly. I'm
having a Force Close issue now that I can't resolve, and I am having
problems debugging it because, well, I'm new to eclipse and android
development in general and just don't really know how.

Here's what I'm doing:

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mDbHelper = new FlashcardDbAdapter(this);
mDbHelper.open();

setContentView(R.layout.databaseupdate);
List<FlashCard> flashcards = GetUpdateXmlFile("http://url.com/
flashcardUpdates.xml
");
TextView txt = (TextView) findViewById(R.id.textViewXmlOut);

String outtext = "";
for (int i = 0; i < flashcards.size(); ++i)
{
try
{
String uid = flashcards.get(i).UniqueId;
Cursor dbCard = mDbHelper.fetchFlashCard(uid);
startManagingCursor(dbCard);
int dbCardVersion = dbCard.getInt(dbCard.getColumnIndexOrThrow
(FlashcardDbAdapter.KEY_VERSION));
if(dbCardVersion < flashcards.get(i).Version)
{
mDbHelper.updateQuestion(flashcards.get(i).UniqueId,
flashcards.get(i).Question, flashcards.get(i).Answer, flashcards.get
(i).Version);
}
else if(flashcards.get(i).Version == -1)
{
mDbHelper.deleteQuestion(flashcards.get(i).UniqueId);
}
dbCard.close();
}
catch(IllegalArgumentException ex)
{
mDbHelper.createQuestion(flashcards.get(i).UniqueId, flashcards.get
(i).Question, flashcards.get(i).Answer, flashcards.get(i).Version);
}
catch(SQLException ex)
{
Log.e(TAG,ex.toString(),ex);
}
catch(Exception e)
{
Log.e(TAG,e.toString(),e);
}
}

Cursor dbCards = mDbHelper.fetchAllFlashCards();
startManagingCursor(dbCards);

dbCards.moveToFirst();

for(int j = 0;j < dbCards.getCount(); ++j)
{
String dbCardUniqueId = dbCards.getString
(dbCards.getColumnIndexOrThrow(FlashcardDbAdapter.KEY_UNIQUEID));
int dbCardVersion = dbCards.getInt(dbCards.getColumnIndex
(FlashcardDbAdapter.KEY_VERSION));
String dbCardQuestion = dbCards.getString
(dbCards.getColumnIndexOrThrow(FlashcardDbAdapter.KEY_QUESTION));
String dbCardAnswer = dbCards.getString
(dbCards.getColumnIndexOrThrow(FlashcardDbAdapter.KEY_ANSWER));

outtext += "UniqueID: " + dbCardUniqueId + "\nVersion: " +
dbCardVersion + "\nQuestion: " + dbCardQuestion + "\nAnswer: " +
dbCardAnswer + "\n-----\n";
dbCards.moveToNext();
}
dbCards.close();
txt.setText(outtext);
}

The problem line seems to be:
int dbCardVersion = dbCard.getInt(dbCard.getColumnIndexOrThrow
(FlashcardDbAdapter.KEY_VERSION));

I had to step through in order to find that because I can't figure out
how to log any output. The Log.e() gives output nowhere that I can
see, and System.out.println ()0doesn't seem to work either...
When it executes that line, it drops out of the try and into the catch
(Exception e) block with the following popping up in eclipse's hover-
over on the "e" object:
android.database.CursorIndexOutOfBoundsException: Index 0 requested,
with a size of 0

Again, because I can't figure out how to log, that's the only way I
could find the exception.

I can't figure out why it's doing this. I've checked all the code I
have and checked it against how the notepad example works and as far
as I can tell it shouldn't have any problems at all. Can anyone shed
some light on this issue? Thanks.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment