Error. Make sure the cursor is initialized correctly before accessing data from it?

I initialize the cursor as follows:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //...Code, code, code... c = db.query("US_States", null, null, null, null, null, null, null); } 

The cursor itself is used in a separate method within a single action:

 public void GameStart() { int gameCount = 0; while(gameCount < 5) { cursorEntry = new Random().nextInt(c.getCount()); c.moveToPosition(cursorEntry); cursorState = c.getString(1); cursorCapital = c.getString(2); displayText.setText(cursorState); 

This gives me the following error:

 E/CursorWindow﹕ Failed to read row 20, column 2 from a CursorWindow which has 50 rows, 2 columns. 

With a stack trace pointing to this line cursorCapital = c.getString(2); every time you restart the application. An error always appears here.

The database is something like this:

 State|Capital Alabama|Montgomery Alaska|Juneau Arizona|Phoenix ...The rest of the states 

I read a couple of similar posts about SO, but they did not give me an idea of ​​what was going wrong. Any input is appreciated.

+6
source share
1 answer

The cursor column index is zero based, so change:

  cursorState = c.getString(1); cursorCapital = c.getString(2); 

to

 cursorState = c.getString(0); cursorCapital = c.getString(1); 
+12
source

All Articles