Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8384723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:28:45+00:00 2026-06-09T17:28:45+00:00

Why might Cursor.getLong() and Cursor.getString() work correctly on an emulator but not an actual

  • 0

Why might Cursor.getLong() and Cursor.getString() work correctly on an emulator but not an actual device running the same version of Android?

I made some additions to version three of Android’s Notepad tutorial that involve programmatically changing notes’ titles. (I recently asked about this project at Obtaining the current Android view and forcing it to be redrawn.) My changes work fine in my emulator, but cause an app-ending “Unfortunately, program has stopped” on my testing phone.

Here’s the method where this is happening:

public void expandNoteTitle(int i) {
    Cursor note = fetchNote(i);
    long rowId =
      note.getLong(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_ROWID));
    String title =
      note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)) + "W";
    String body =
      note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY));
    updateNote(rowId, title, body);
}

I know that fetching the cursor is at least partially working because note.getColumnName(1) works fine in the debugger. I also confirmed that the arguments to getLong() and getString() show up correctly.

Here’s the LogCat output:

08-12 15:35:29.735: D/AndroidRuntime(17937): Shutting down VM
08-12 15:35:29.735: W/dalvikvm(17937): threadid=1: thread exiting with uncaught exception (group=0x40a3d1f8)
08-12 15:35:29.751: E/AndroidRuntime(17937): FATAL EXCEPTION: main
08-12 15:35:29.751: E/AndroidRuntime(17937): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
08-12 15:35:29.751: E/AndroidRuntime(17937):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
08-12 15:35:29.751: E/AndroidRuntime(17937):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
08-12 15:35:29.751: E/AndroidRuntime(17937):    at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74)
08-12 15:35:29.751: E/AndroidRuntime(17937):    at notepad.NotesDbAdapter.expandNoteTitle(NotesDbAdapter.java:212)
08-12 15:35:29.751: E/AndroidRuntime(17937):    at notepad.NotesDbAdapter.expandNoteTitles(NotesDbAdapter.java:201)
08-12 15:35:29.751: E/AndroidRuntime(17937):    at notepad.NotepadMain.expandTitles(NotepadMain.java:167)
08-12 15:35:29.751: E/AndroidRuntime(17937):    at notepad.NotepadMain.onMenuItemSelected(NotepadMain.java:115)
08-12 15:35:29.751: E/AndroidRuntime(17937):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
08-12 15:35:29.751: E/AndroidRuntime(17937):    at [SNIPPED FOR LENGTH]
08-12 15:35:29.751: E/AndroidRuntime(17937):    at dalvik.system.NativeStart.main(Native Method)
08-12 15:36:49.993: I/Process(17937): Sending signal. PID: 17937 SIG: 9

And here’s the code for fetchNote(). It is the same as the version given in the tutorial except for a variable name.

public Cursor fetchNote(long rowId) throws SQLException {
    Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
      KEY_ROWID, KEY_TITLE, KEY_BODY }, KEY_ROWID + "=" + rowId, null,
      null, null, null, null);
    if(mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:28:46+00:00Added an answer on June 9, 2026 at 5:28 pm

    Cursor’s getLong() and getString() work on an emulator but not an equivalent device… I know that fetching the cursor is at least partially working because note.getColumnName(1) works fine in the debugger. I also confirmed that the arguments to getLong() and getString() show up correctly.

    Cursor’s getLong() and getString() methods work identically on both emulators and actual devices.

    If you look at your LogCat, you’ll see the cause of your problem is that there is no data:

    android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

    It appears that the Cursor is empty, does your table (on the device) have any rows?


    Added from comments

    As we discovered you made the assumption that all of your row id’s were sequential, but that is not true. Here are my two suggestions to fetch the right data and prevent any future force closes from your Cursors:

    1. Fetch all of the existing row ids and iterate through them:

      Cursor notes = database.query(DATABASE_TABLE, new String[] { KEY_ROWID }, null, null, null, null, null, null);
      
      while(notes.moveToNext()) 
          expandNoteTitle(notes.getLong(0)); 
      

      To iterate through a new Cursor you only to check the return value from moveToNext(). When the Cursor’s index is set to -1 (like when it’s new), moveToNext() will move to index 0 and return true only if there is valid data, otherwise if the are no rows moveToNext() returns false.

    2. Always assume that a Cursor could be empty and understand that an empty cursor does not equal null:

      public Cursor fetchNote(long rowId) throws SQLException {
          return database.query(true, DATABASE_TABLE, 
                  new String[] { KEY_ROWID, KEY_TITLE, KEY_BODY }, 
                  KEY_ROWID + "=" + rowId, null, null, null, null, null);
      }
      
      public void expandNoteTitle(long rowId) {
          Cursor note = fetchNote(rowId);
          if(note.getCount() > 0) {
              // You could also use if(cursor.moveToNext()) or if(cursor.moveToFirst()), both of these return true / false depending on whether there is valid data
      
              String title =
                note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)) + "W";
              String body =
                note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY));
              updateNote(rowId, title, body);
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might help someone but its not 100% working. I've tried to stop the
Might be my question sounds more naive. But I wanted to know if it
Might be my question is abstract or out of context, but i am asking
Might be a bit of noob question but it's something that's been getting me
might be a stupid question but I seem to be confused. Im pretty new
Might be a stupid question but in our iOS app we're storing an integer
I might have a flawed understanding of what shared_examples_for should do, but hear me
This might seem like a very easy question for some of you folks, but
It might look like a stupid question, but I recently realized that .NET implementation
This might be a simple question but I've searched and searched and can't find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.