0

Ï developed a little SQLite database to add a list of goods, when I fletchallgoods through the database I have an error that says the index is not valid:

the code that generate the error:

...
Cursor goodsCursor = goodsfletcher.fetchAllGoods();
String goods[] = new String[goodsCursor.getCount()] ;
TableLayout tl = (TableLayout) findViewById(R.id.baskettable);
for(int i=0;i<=goods.length;i++){
    String Goodname = goodsCursor.getString(1).toString() ;
...

the clss Dbadapter that handle the conexion:

public Cursor fetchAllGoods() {

        return mDb.query(DATABASE_TABLE, new String[] {KEY_CODART, KEY_ARTI,
                KEY_PROV, KEY_PRECBOL, KEY_QUANT}, null, null, null, null, null);
}

The log cat of the error:

03-14 16:19:37.324: E/AndroidRuntime(5690): FATAL EXCEPTION: main
03-14 16:19:37.324: E/AndroidRuntime(5690): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.remotedata.firstapp/com.remotedata.firstapp.chartView}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
03-14 16:19:37.324: E/AndroidRuntime(5690):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at android.os.Looper.loop(Looper.java:130)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at android.app.ActivityThread.main(ActivityThread.java:3835)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at java.lang.reflect.Method.invoke(Method.java:507)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at dalvik.system.NativeStart.main(Native Method)
03-14 16:19:37.324: E/AndroidRuntime(5690): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
03-14 16:19:37.324: E/AndroidRuntime(5690):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at com.remotedata.firstapp.chartView.onCreate(chartView.java:36)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-14 16:19:37.324: E/AndroidRuntime(5690):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
03-14 16:19:37.324: E/AndroidRuntime(5690):     ... 11 more
1
  • How many columns is the cursor returning? Commented Mar 14, 2012 at 20:32

1 Answer 1

3

You need to move to the first record in your Cursor before retrieving values.

Ex.

Cursor goodsCursor = goodsfletcher.fetchAllGoods();
String goods[] = new String[goodsCursor.getCount()] ;
TableLayout tl = (TableLayout) findViewById(R.id.baskettable);
if (c.moveToFirst()){
    do {
        String Goodname = goodsCursor.getString(1).toString() ;
    }while (c.moveToNext());


}

c.close();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.