Search code examples
javaandroidcursorindexoutofboundsexceptiontoast

Why am I having such an error to my code? CursorIndexOutOfBoundsException


Is there anyone who can try to find the mistake in my code? I tried many things already but it always ends up with the same error.

This is my code:

                String signeedep = spn.getSelectedItem().toString();
                int a = spn1.getSelectedItemPosition();
                Cursor userid = db.getallsigneenumber();

                    userid.moveToPosition(a);
                    Toast.makeText(getApplicationContext(),
                             userid.getString(1) + "//this is where the error points out
                             was assigned to the " + signeedep + ".",Toast.LENGTH_SHORT).show();

                    userid.close();
                /*
                db.open();
                db.insertAssignsignee(signeedep, userid.getString(0));
                db.close();
                */
                    myMethod();

This is my error:

FATAL EXCEPTION: main

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

Solution

  • First make sure your cursor is not null and it is having required number of element in it..

    remember moveToPosition(1) returns the second row, since it's zero-indexed