Fionnuala
Fionnuala

Reputation: 77

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

i am aware that there is a lot of threads about this exception however, i would like people to view my code and assist me in where i am specifically going wrong.

Here is my code for trying to store my users input to an sqlite database and then transfer the code and display it in a new activity. Can anyone tell me where i am going wrong and why this error keeps occuring? (error in title)

   Cursor res = myDb.getAllData();


            StringBuffer buffer = new StringBuffer();

            String getid = res.getString(0);
            String getlt = res.getString(1);
            String getpt = res.getString(2);
            String getqty = res.getString(3);
            String getdur = res.getString(4);
            String getst = res.getString(5);
            String getet = res.getString(6);


            if ( res != null && res.moveToFirst()) {
                do {
                    buffer.append(getid);
                    buffer.append(getlt);
                    buffer.append(getpt);
                    buffer.append(getqty);
                    buffer.append(getdur);
                    buffer.append(getst);
                    buffer.append(getet);
                } while (res.moveToNext());
            }


            Intent TransferData = new Intent(getBaseContext(), screen4.class);
            TransferData.putExtra("ID", getid);
            TransferData.putExtra("LineType", getlt);
            TransferData.putExtra("PackageType", getpt);
            TransferData.putExtra("Quantity", getqty);
            TransferData.putExtra("Duration", getdur);
            TransferData.putExtra("Starttime",getst);
            TransferData.putExtra("endtime", getet);

            startActivity(TransferData);
        }
    });
}

}

ATTEMPT 2 INSIDE DO BLOCK :

 Cursor res = myDb.getAllData();


            StringBuffer buffer = new StringBuffer();


            if ( res != null && res.moveToNext()) {
                do {

                    String getid = res.getString(0);
                    String getlt = res.getString(1);
                    String getpt = res.getString(2);
                    String getqty = res.getString(3);
                    String getdur = res.getString(4);
                    String getst = res.getString(5);
                    String getet = res.getString(6);

                    buffer.append(getid);
                    buffer.append(getlt);
                    buffer.append(getpt);
                    buffer.append(getqty);
                    buffer.append(getdur);
                    buffer.append(getst);
                    buffer.append(getet);

                    Intent TransferData = new Intent(getBaseContext(), screen4.class);
                    TransferData.putExtra("ID", getid);
                    TransferData.putExtra("LineType", getlt);
                    TransferData.putExtra("PackageType", getpt);
                    TransferData.putExtra("Quantity", getqty);
                    TransferData.putExtra("Duration", getdur);
                    TransferData.putExtra("Starttime",getst);
                    TransferData.putExtra("endtime", getet);

                    startActivity(TransferData);

                } while (res.moveToNext());

Upvotes: 1

Views: 100

Answers (1)

Nabin Bhandari
Nabin Bhandari

Reputation: 16429

What you are trying to do is a unclear.

In your code there is:

String getid = res.getString(0);
String getlt = res.getString(1);
//bla bla bla

I believe they should be inside the do block.

You have tried to access the column data without moving the Cursor to first position.

You must call Cursor#moveToFirst(); before reading any value from the Cursor.

Upvotes: 1

Related Questions