dasdasd
dasdasd

Reputation: 2031

Make sure the Cursor is initialized correctly before accessing data from it

Im trying to get data from my DB.

This is my code:

String[] columns = new String[] {COLUMN_FACEBOOK_ALBUM_COVER, COLUMN_FACEBOOK_ALBUM_IS_ACTIVE};

        String whereClause = COLUMN_FACEBOOK_ALBUM_IS_ACTIVE + "= '" + 1 + "'";

        Cursor cAlbum = ourDatabase.query(TABLE_FACEBOOK, columns, whereClause,null, null, null, null);

        columns = new String[] {COLUMN_FACEBOOK_BIG_IMAGE, COLUMN_FACEBOOK_IMAGE_IS_ACTIVE};

        whereClause = COLUMN_FACEBOOK_IMAGE_IS_ACTIVE + "= '" + 1 + "'";

        Cursor cImage = ourDatabase.query(TABLE_FACEBOOK_IMAGES, columns, null,null, null, null, null);

        String[] list = new String[cAlbum.getCount()+cImage.getCount()];

        int p = 0;

        if(cAlbum.getCount() < 1 && cImage.getCount() < 1)
        {
            cAlbum.close();
            cImage.close();
            return null;
        }

        Log.i("cImage length", cImage.getCount()+"");
        Log.i("cAlbum length", cAlbum.getCount()+"");

        Log.i("list length", list.length+"");

        int i = cImage.getColumnIndex(COLUMN_FACEBOOK_BIG_IMAGE);
        int j = cAlbum.getColumnIndex(COLUMN_FACEBOOK_ALBUM_COVER);

        for (cAlbum.moveToFirst(); !cAlbum.isAfterLast(); cAlbum.moveToNext())
        {
            list[p] = cAlbum.getString(j);
            p++;
        }

        for (cImage.moveToFirst(); !cImage.isAfterLast(); cImage.moveToNext())
        {
            list[p] = cImage.getString(i);
            Log.i("image length", list[p].length()+"");
            p++;
        }

        cAlbum.close();
        cImage.close();

        return list;

The code fails on this line:

list[p] = cImage.getString(i);

The error message is:

11-27 12:34:59.683: E/CursorWindow(6901): Failed to read row 2, column 0 from a CursorWindow which has 2 rows, 2 columns.
11-27 12:34:59.683: D/AndroidRuntime(6901): Shutting down VM
11-27 12:34:59.683: W/dalvikvm(6901): threadid=1: thread exiting with uncaught exception (group=0x41e9b930)
11-27 12:34:59.693: E/AndroidRuntime(6901): FATAL EXCEPTION: main
11-27 12:34:59.693: E/AndroidRuntime(6901): java.lang.RuntimeException: Unable to create service com.example.imageswidget.WidgetService: java.lang.IllegalStateException: Couldn't read row 2, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
11-27 12:34:59.693: E/AndroidRuntime(6901):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2667)
11-27 12:34:59.693: E/AndroidRuntime(6901):     at android.app.ActivityThread.access$1600(ActivityThread.java:153)
11-27 12:34:59.693: E/AndroidRuntime(6901):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
11-27 12:34:59.693: E/AndroidRuntime(6901):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 12:34:59.693: E/AndroidRuntime(6901):     at android.os.Looper.loop(Looper.java:137)
11-27 12:34:59.693: E/AndroidRuntime(6901):     at android.app.ActivityThread.main(ActivityThread.java:5227)
11-27 12:34:59.693: E/AndroidRuntime(6901):     at java.lang.reflect.Method.invokeNative(Native Method)
11-27 12:34:59.693: E/AndroidRuntime(6901):     at java.lang.reflect.Method.invoke(Method.java:511)
11-27 12:34:59.693: E/AndroidRuntime(6901):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-27 12:34:59.693: E/AndroidRuntime(6901):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-27 12:34:59.693: E/AndroidRuntime(6901):     at dalvik.system.NativeStart.main(Native Method)
11-27 12:34:59.693: E/AndroidRuntime(6901): Caused by: java.lang.IllegalStateException: Couldn't read row 2, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
11-27 12:34:59.693: E/AndroidRuntime(6901):     at android.database.CursorWindow.nativeGetString(Native Method)
11-27 12:34:59.693: E/AndroidRuntime(6901):     at android.database.CursorWindow.getString(CursorWindow.java:434)
11-27 12:34:59.693: E/AndroidRuntime(6901):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
11-27 12:34:59.693: E/AndroidRuntime(6901):     at com.example.imageswidget.DataBaseMain.getFacebookImagesForWidget(DataBaseMain.java:943)
11-27 12:34:59.693: E/AndroidRuntime(6901):     at com.example.imageswidget.WidgetService.onCreate(WidgetService.java:51)
11-27 12:34:59.693: E/AndroidRuntime(6901):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2657)
11-27 12:34:59.693: E/AndroidRuntime(6901):     ... 10 more

For the call log

Log.i("image length", list[p].length()+"");

I get few calls before the code breaks. The problem seems to happen only for some rows but the error code does not tell me what the problem is.

Thanks for helping.

Upvotes: 2

Views: 34814

Answers (7)

Niharika Gautam
Niharika Gautam

Reputation: 39

You should fix this to solve the following error message

Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

It works in my code.

  1. Try to position cursor by moveToFirst before reading data from it.

  2. Check for null-> if (c != null && c.moveToFirst()) {}

  3. Check for count-> (c != null && c.getCount() >0 && c.moveToFirst()){}

Upvotes: 3

okarakose
okarakose

Reputation: 3742

Maybe you are using your old database. If you changed some tables, fields on your database and you are now trying on another device (which is you already installed your application before database changes), you should only delete your old database and create new file.

Upvotes: 5

nww04
nww04

Reputation: 1857

I have similar answer as Oladipo Olasemo but happens to me for v-tables and how I was able to fix it. I believe this is the best post to share my experience with this error especially dealing with FTS3 virtual tables, well at least in my case. There are a lot of question regarding this and OP's original question is too generic so here is my take on this question.

I encountered this error due to some restrictions in virtual table consider this one:

create virtual table V1 using FTS3 (content="MyTable", content1, content2); 

When I search for a specific keyword base on content1 or content2, I realize I was making a query base on the virtual table via this query:

select * from V1 where V1 match 'hello'

Since this is a virtual table I missed that, it will project the result set according to how you constructed the v-table itself. So it will have columns only for content1 and content2.

I am developing an android app which queries from search results and upon selecting an item from the result set, my backend processing process the selected item and queries for an information that does not exist from the column set. Thus, giving me this error.

The way I solved this, though not so brilliant, is to querythe result from the v-table to get the id for each item, which it always has. Then I construct a query string base from the result ids I acquired then use that to retrieve the necessary data with the projection I required. I got an idea from this question

I believe there are lot more ways to encounter this error. I spent almost an hour figuring out what might have caused this. For me, the error projected on the log is not very helpful. It really helps if you really know how data flows within your app.

Upvotes: 1

Oladipo Olasemo
Oladipo Olasemo

Reputation: 2042

In my case it was because I didn't include the column in the projection passed into the CursorLoader constructor. Which means I was trying to access data from a column that didn't exist in the cursor that was returned.

I hope this helps somebody...

Upvotes: 19

Peter
Peter

Reputation: 10970

I have encountered this same exception before. In my case the cause was a database cell containing too much data (a huge string). Judging from the code (COLUMN_FACEBOOK_BIG_IMAGE) I guess that you have encountered the same problem. For the solution was to check the size of the strings that were put into the DB (and down scaling the image when needed). Hope this helps!

Upvotes: 9

appoll
appoll

Reputation: 2950

Try calling cursor.moveToFirst () before using the cursor. Let me know if that worked.

Upvotes: 3

Boban S.
Boban S.

Reputation: 1662

Initialize cursors in database transaction.

Cursor cAlbum = null, cImage = null;
db.beginTransaction();
try {
    // init cursors
    cAlbum = db.query(...);
    cImage = db.query(...);
} catch(SQLException e) {
    e.printStackTrace();
} finally {
    db.endTransaction();
}

Check if cursors are not null.

if(cAlbum != null && cImage != null) {
    while(cAlbum.moveToNext()) {
        // get data from cAlbum and do what you need
    }
    while(cImage.moveToNext()) {
        // get data from cImage and do what you need
    }

    cAlbum.close();
    cImage.close();
}

Upvotes: -1

Related Questions