Android.database.CursorIndexOutOfBoundsException: index 0 is requested with a size of 0 cursor

I get the following error:

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

From this code:

  public Lirik getLirik(String id){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_MUPUH, 
                             new String[] { KEY_ID, JUDUL, KEY_MUPUH }, 
                             KEY_ID + "=?",
                             new String[] {
                               String.valueOf(id)
                             }, 
                             null, 
                             null, 
                             null);
    if (cursor != null && cursor.moveToFirst())
        cursor.moveToFirst();

    Lirik lirik = new Lirik(cursor.getString(1),
                            cursor.getString(2));
    db.close();
    return lirik;
}

and this class ViewMupuh: error:Lirik lirik = db.getLirik(position);

public class ViewMupuh extends Activity implements OnClickListener {
    private static String position = null;

    private TextView textJudul, textLirik;
    private Button bUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_mupuh);
        Intent intent = getIntent();
        position = intent.getStringExtra("position");
        if (position != null) {
            Log.d("value of position", position);
        }
        DBHelper db = new DBHelper(this);
        Lirik lirik = db.getLirik(position);
        textJudul = (TextView) findViewById(R.id.judul_details);
        textJudul.setText(lirik.getJudul());
        textLirik = (TextView) findViewById(R.id.lirikdetails);
        textLirik.setText(lirik.getLirik());

        bUpdate = (Button) findViewById(R.id.bupdatedetails);
        bUpdate.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getApplicationContext(), EditLirik.class);
        intent.putExtra("position value", position);
        startActivity(intent);

    }
}
+4
source share
2 answers

You have a few problems in your code. First of all, you call cursor.moveToFirst()twice in normal mode:

if (cursor != null && cursor.moveToFirst()) // <-- First call
    cursor.moveToFirst();                   // <-- Second call

In addition, you are trying to get data from the cursor, even if the cursor is empty or it has not received data:

if (cursor != null && cursor.moveToFirst())
    cursor.moveToFirst();

Lirik lirik = new Lirik(cursor.getString(1), 
        cursor.getString(2)); // <-- cursor can be null here

In any case, you leave resources open. You never close cursor, and if there is any problem, dbalso does not close. I suggest rewriting your function getLirik()as follows:

public Lirik getLirik(final String id) {
    Lirik lirik = null;

    SQLiteDatabase db = null;
    Cursor cursor = null;

    try {
        db = this.getReadableDatabase();
        cursor = db.query(TABLE_MUPUH, new String[] { KEY_ID, JUDUL,
                KEY_MUPUH }, KEY_ID + "=?", new String[] { id }, null,
                null, null);
        if (cursor != null && cursor.moveToFirst()) {
            lirik = new Lirik(cursor.getString(1), cursor.getString(2));
        }
    } catch (final Exception e) {
        // Do something with the Exception (Log, raise, ...)
    } finally {
        cursor.close();
        db.close();
    }

    return lirik;
}

, , getLirik() null, ViewMupuh.onCreate() , :

textJudul = (TextView) findViewById(R.id.judul_details);
textLirik = (TextView) findViewById(R.id.lirikdetails);

DBHelper db = new DBHelper(this);
Lirik lirik = db.getLirik(position);

if (lirik != null) {
    textJudul.setText(lirik.getJudul());
    textLirik.setText(lirik.getLirik());
}

, , , , (, ?)

+3

, . , . .

    if (cursor != null && cursor.moveToFirst())
    cursor.moveToFirst();

    Lirik lirik = new Lirik(cursor.getString(1),
            cursor.getString(2));
    db.close();
    return lirik;

if . , , Lirik lirik = new Lirik(cursor.getString(1), cursor.getString(2));

, , .

-1

All Articles