0

I get an error when I try to retrieve from SQLite into an Array. I identified the error cause in the for loop, error in the statement

urlString[i] = allrows.getString(0);

I tried to debug it but still I do not know how to solve it. The method as below:

public String[] getImgURL(){
        mydb = localContext.openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        Cursor allrows  = mydb.rawQuery("SELECT ImgURL FROM "+  TABLE, null);
        String[] urlString = new String[allrows.getCount()-1];
        for (int i = 1; i < allrows.getCount(); i++) {
            urlString[i] = allrows.getString(0);
            allrows.moveToNext();               
        }
        allrows.close();    
        mydb.close();
        return urlString;
}

the log as follow

03-06 01:26:05.479: W/dalvikvm(27671): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)    
03-06 01:26:05.479: E/AndroidRuntime(27671): FATAL EXCEPTION: main    
03-06 01:26:05.479: E/AndroidRuntime(27671): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.testing/com.ip.scan.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 5    
03-06 01:26:05.479: E/AndroidRuntime(27671):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)    
03-06 01:26:05.479: E/AndroidRuntime(27671):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)    
03-06 01:26:05.479: E/AndroidRuntime(27671):    at android.app.ActivityThread.access$1500(ActivityThread.java)    
03-06 01:26:05.479: E/AndroidRuntime(27671):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java)    
03-06 01:26:05.479: E/AndroidRuntime(27671):    at android.os.Handler.dispatchMessage(Handler.java)    
03-06 01:26:05.479: E/AndroidRuntime(27671):    at android.os.Looper.loop(Looper.java)    
03-06 01:26:05.479: E/AndroidRuntime(27671):    at android.app.ActivityThread.main(ActivityThread.java)
03-06 01:26:05.479: E/AndroidRuntime(27671):    at java.lang.reflect.Method.invokeNative(Native Method)
03-06 01:26:05.479: E/AndroidRuntime(27671):    at java.lang.reflect.Method.invoke(Method.java:507)
03-06 01:26:05.479: E/AndroidRuntime(27671):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
03-06 01:26:05.479: E/AndroidRuntime(27671):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
03-06 01:26:05.479: E/AndroidRuntime(27671):    at dalvik.system.NativeStart.main(Native Method)
03-06 01:26:05.479: E/AndroidRuntime(27671): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 5
03-06 01:26:05.479: E/AndroidRuntime(27671):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java)
03-06 01:26:05.479: E/AndroidRuntime(27671):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java)
03-06 01:26:05.479: E/AndroidRuntime(27671):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java)
03-06 01:26:05.479: E/AndroidRuntime(27671):    at com.ip.scan.LocalSQLite.getImgURL(LocalSQLite.java:57)
03-06 01:26:05.479: E/AndroidRuntime(27671):    at com.ip.scan.MainActivity.onCreate(MainActivity.java:32)
03-06 01:26:05.479: E/AndroidRuntime(27671):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
03-06 01:26:05.479: E/AndroidRuntime(27671):    ... 12 more
03-06 01:26:05.549: D/dalvikvm(27671): GC_CONCURRENT freed 220K, 41% free 3334K/5639K, external 0K/0K, paused 3ms+2ms
03-06 01:26:05.549: E/Database(27671): close() was never explicitly called on database '/data/data/com.test.testing/databases/vege.db' 
03-06 01:26:05.549: E/Database(27671): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
03-06 01:26:05.549: E/Database(27671):  at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java)
03-06 01:26:05.549: E/Database(27671):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java)
03-06 01:26:05.549: E/Database(27671):  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java)
03-06 01:26:05.549: E/Database(27671):  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java)
03-06 01:26:05.549: E/Database(27671):  at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java)
03-06 01:26:05.549: E/Database(27671):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java)
03-06 01:26:05.549: E/Database(27671):  at com.ip.scan.LocalSQLite.getImgURL(LocalSQLite.java:53)
03-06 01:26:05.549: E/Database(27671):  at com.ip.scan.MainActivity.onCreate(MainActivity.java:32)
03-06 01:26:05.549: E/Database(27671):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
03-06 01:26:05.549: E/Database(27671):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
03-06 01:26:05.549: E/Database(27671):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
03-06 01:26:05.549: E/Database(27671):  at android.app.ActivityThread.access$1500(ActivityThread.java)
03-06 01:26:05.549: E/Database(27671):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
03-06 01:26:05.549: E/Database(27671):  at android.os.Handler.dispatchMessage(Handler.java)
03-06 01:26:05.549: E/Database(27671):  at android.os.Looper.loop(Looper.java)
03-06 01:26:05.549: E/Database(27671):  at android.app.ActivityThread.main(ActivityThread.java)
03-06 01:26:05.549: E/Database(27671):  at java.lang.reflect.Method.invokeNative(Native Method)
03-06 01:26:05.549: E/Database(27671):  at java.lang.reflect.Method.invoke(Method.java:507)
03-06 01:26:05.549: E/Database(27671):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
03-06 01:26:05.549: E/Database(27671):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
03-06 01:26:05.549: E/Database(27671):  at dalvik.system.NativeStart.main(Native Method)
1
  • 1) can we get a stack trace? 2) I generally use do while loops to iterate through my cursors, although I guess I can see why here. Commented Mar 5, 2012 at 17:23

4 Answers 4

1

Try this,

 public String[] getImgURL(){
    mydb = localContext.openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
    Cursor allrows  = mydb.rawQuery("SELECT ImgURL FROM "+  TABLE, null);
    String[] urlString = new String[allrows.getCount()-1];

    allrows.moveToFirst();

    for (int i = 1; i < allrows.getCount(); i++) {
        urlString[i] = allrows.getString(0);
        allrows.moveToNext();               
    }
    allrows.close();    
    mydb.close();
    return urlString;

}

Sign up to request clarification or add additional context in comments.

Comments

0

2 problems, here.

First, you must start by moving your cursor. moveToFirst or moveToNext is fine.

Secondly, your index in your array as well as the size of the array is incorrect.

A proper and current of doing this is as follows:

mydb = localContext.openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
Cursor allrows  = mydb.rawQuery("SELECT ImgURL FROM "+  TABLE, null);
List<String> result = new ArrayList<String>();
while (allrows.moveToNext()) {
    result.add(allrows.getString(0));
}
allrows.close();    
mydb.close();
return allrows.toArray(new String[0]);

Comments

0

If your error is an index out of bounds, my guess would be that you are creating an array with size n -1 and then putting n elements inside. It should be

String[] urlString = new String[allrows.getCount()];

That way the array is large enough for the number of elements you are putting inside. If this is woefully wrong, please post a stacktrace, stabbing in the dark is not much fun.

1 Comment

OR i should start at 0, because arrays are 0 indexed
0

Before you start operating with the cursor you need to call allrows.moveToFirst.

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.