Search code examples
android-sqlite

How to fix " java.lang.IllegalStateException: Couldn't read row 2, col 7 from CursorWindow."


When I insert the database from SQLite into Music Adapter using CursorWindow, it will report an error "java.lang.IllegalStateException: Couldn't read row 0, col 7 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it."

This is for Android Studio 3.3. In the past, I've tried on Inserting and exporting data from SQlite to ArrayAdapter for Listview and errors often occur: "java.lang.IllegalStateException: Couldn't read row 0, col 7 from CursorWindow"

This is my code:

public class MusicAdapter extends ArrayAdapter<Music>
{
    Activity context;
    int resource;
    List<Music> objects;

     int Like =0; 

    public MusicAdapter(Activity context, int resource, List<Music> objects)
    {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
        this.objects = objects;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = this.context.getLayoutInflater();
        View row = inflater.inflate(this.resource,null);

        TextView txtMa = row.<TextView>findViewById(R.id.txtMa);
        TextView txtTen = row.<TextView>findViewById(R.id.txtTen);
        TextView txtCaSi = row.<TextView>findViewById(R.id.txtCaSi);

        final TextView txtLike = row.<TextView>findViewById(R.id.txtLike);        final TextView txtDisLike = row.<TextView>findViewById(R.id.txtDisLike); 
        ImageButton btnLike = row.<ImageButton>findViewById(R.id.btnLike);
        ImageButton btnDisLike = row.<ImageButton>findViewById(R.id.btnDisLike);

        final Music music = this.objects.get(position);
        txtTen.setText(music.getTen());
        txtMa.setText(music.getMa());
        txtCaSi.setText(music.getCaSi());


        btnLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                xuLyThich(music, position,txtLike);

            }
        });

        btnDisLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                xuLyKhongThich(music,position,txtDisLike);

            }
        });

        return row;
    }

    private void xuLyKhongThich(Music music, int pos,TextView txtDisLike)
    {

        int no_un_like =0;
        Cursor cursor=MainActivity.database.query("ArirangSongList",null,
                null,null,
                null,null,null);


        try {
            if (cursor!= null) {
                cursor.move(pos+1);
                no_un_like = cursor.getInt(8);
                Log.d("no_unlike",String.valueOf(no_un_like));
            }

        } finally {
            cursor.close();
        }

        ContentValues row = new ContentValues();

        row.put("Dislike",  no_un_like+1);


        try{
            MainActivity.database.update("ArirangSongList", row, "MABH= ?", new String[]{String.valueOf(music.getMa())});
            txtDisLike.setText(String.valueOf(no_un_like+1));
        }finally {

        }
    }

    private void xuLyThich(Music music, int pos,TextView txtlike)
    {
         int no_like =0;
        Cursor cursor=MainActivity.database.query("ArirangSongList",null,
                null,null,
                null,null,null);


        try {
            if (cursor!= null) {
                cursor.move(pos+1);
                no_like = cursor.getInt(7);
                Log.d("no_like",String.valueOf(no_like));
            }

        } finally {
            cursor.close();
        }

        ContentValues row = new ContentValues();

        row.put("Like",  no_like+1);


        try{
            MainActivity.database.update("ArirangSongList", row, "MABH= ?", new String[]{String.valueOf(music.getMa())});

            txtlike.setText(String.valueOf(no_like+1));

        }finally {

        }
    }

}

And this is my error:

java.lang.IllegalStateException: Couldn't read row 2, col 7 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.database.CursorWindow.nativeGetLong(Native Method)
        at android.database.CursorWindow.getLong(CursorWindow.java:507)
        at android.database.CursorWindow.getInt(CursorWindow.java:574)
        at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:69)
        at muitenvang.adapter.MusicAdapter.xuLyThich(MusicAdapter.java:136)
        at muitenvang.adapter.MusicAdapter.access$000(MusicAdapter.java:23)
        at muitenvang.adapter.MusicAdapter$1.onClick(MusicAdapter.java:74)
        at android.view.View.performClick(View.java:4204)
        at android.view.View$PerformClick.run(View.java:17355)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

Solution

  • You issue is that your are trying to read a row from the Cursor that doesn't exist.

    This is due to the position in the list not being an offset but being the sequential number. That is the first position is 1 whilst the equivalent row in the cursor would be 0 (position 2 would correlate with row 1 and so on).

    Adding 1 to the position as per cursor.move(pos+1); makes the exception more likely to occur as does not checking the result of the move (false if the move could not be made else true) to see if the move succeeded.

    Checking a Cursor, returned from an SQLiteDatabase method, such as query fir null, is useless as the Cursor will not be null. The Cursor would, if there are no rows, still be valid but the count, as could be checked with the Cursor getCount method would return 0 (or the number of rows in the Cursor).

    Although not ideal changing :-

            if (cursor!= null) {
                cursor.move(pos+1);
                no_like = cursor.getInt(7);
                Log.d("no_like",String.valueOf(no_like));
            }
    

    To :-

            if (cursor.move(pos-1) {
                no_like = cursor.getInt(7);
                Log.d("no_like",String.valueOf(no_like));
            } else {
                Log.d("no_like","Ooops could not move to row + String.valueOf(pos));
            }
    

    Would be more likely to work.

    • Note the same changes should be applied to xuLyKhongThich