0

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

I want to get the photo name from SQL database,then get the photo from drawable file and link the photo to a ImageView item which is inside a ListView what is the problem with following code, Please help me fix it, thank you

import android.app.ListActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class HotProduct extends ListActivity {

    private ProductSQLiteHelper sqlHelper;
    private SQLiteDatabase infodb;
    private String[] columnsToSelect;
    private String[] columnsToSelect2;
    private Cursor infoCursor;
    private SimpleCursorAdapter dbAdapter;
    private SimpleCursorAdapter dbAdapter2;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hotproduct_middle_page);
        columnsToSelect = new String[] {
                ProductSQLiteHelper.PRODUCT_ID,
                ProductSQLiteHelper.PRODUCT_NAME,
                ProductSQLiteHelper.PRODUCT_BRAND,
                ProductSQLiteHelper.PRODUCT_PRICE


        };




        Resources res = getResources();
        Log.v("Res:",String.valueOf(res));


//        setTitle("Hot Product");



        sqlHelper = new ProductSQLiteHelper(this);

        infodb = sqlHelper.getReadableDatabase();


        Cursor infoCursor= infodb.rawQuery("SELECT photo FROM phone", null);
        ImageView moviePoster = (ImageView) findViewById(R.id.productPhoto);
        infoCursor.moveToFirst();
        if (!infoCursor.isAfterLast()) {
            if (infoCursor.getInt(infoCursor.getColumnIndex(String.valueOf(ProductSQLiteHelper.PRODUCT_HOT))) == 1) {
                res = this.getResources();
                String posterResourceName = infoCursor.getString(infoCursor.getColumnIndex(ProductSQLiteHelper.PRODUCT_PHOTO));
                Log.v("resource name",posterResourceName);
                int resId = res.getIdentifier(posterResourceName, "drawable", getPackageName());
                moviePoster.setImageResource(resId);
            }
        }

        String columnsToDisplay[] = {
                ProductSQLiteHelper.PRODUCT_NAME,
                ProductSQLiteHelper.PRODUCT_BRAND,
                ProductSQLiteHelper.PRODUCT_PRICE
//                ProductSQLiteHelper.PRODUCT_PHOTO
        };


        int mappingToView[] = {
                R.id.productName,
                R.id.productBrand,
                R.id.productPrice
//                R.id.productPhoto
        };





        String args[] = {"1"};
        infoCursor = infodb.query(ProductSQLiteHelper.TABLE_NAME,columnsToSelect," hot = ? ",args,null,null,null);

        String posterResourceName = infoCursor.getString(infoCursor.getColumnIndex("photo"));
        Log.v("resource name",posterResourceName);
        dbAdapter = new SimpleCursorAdapter(this,R.layout.hotproduct_middle_page_row,infoCursor,columnsToDisplay,mappingToView,0);


        setListAdapter(dbAdapter);



        }




}

I found that the problems appear in

      Cursor infoCursor= infodb.rawQuery("SELECT photo FROM phone", null);
        ImageView moviePoster = (ImageView) findViewById(R.id.productPhoto);
        infoCursor.moveToFirst();
        if (!infoCursor.isAfterLast()) {
            if (infoCursor.getInt(infoCursor.getColumnIndex(String.valueOf(ProductSQLiteHelper.PRODUCT_HOT))) == 1) {
                res = this.getResources();
                String posterResourceName = infoCursor.getString(infoCursor.getColumnIndex(ProductSQLiteHelper.PRODUCT_PHOTO));
                Log.v("resource name",posterResourceName);
                int resId = res.getIdentifier(posterResourceName, "drawable", getPackageName());
                moviePoster.setImageResource(resId);
            }
        }
InsaneCat
  • 2,115
  • 5
  • 21
  • 40
  • i think you have to check before write this lines `String args[] = {"1"}; infoCursor = infodb.query(ProductSQLiteHelper.TABLE_NAME,columnsToSelect," hot = ? ",args,null,null,null); ` , that `if (cur.moveToFirst()) { String posterResourceName = infoCursor.getString(infoCursor.getColumnIndex("photo")); }` please try like this. – KuLdip PaTel Dec 08 '17 at 13:23
  • Possible duplicate of [Android cursor error - "make sure cursor is initialized correctly before accessing data from it..."](https://stackoverflow.com/questions/10483644/android-cursor-error-make-sure-cursor-is-initialized-correctly-before-accessi) – Mitesh Vanaliya Dec 08 '17 at 13:32

1 Answers1

0

Your issue is due to a getColumnIndex not finding the specified column and thus returning -1 (as it does when the column is not a column). The underlying cause is that you are creating a Cursor with a single column yet in your code you are trying to access 2 columns i.e.

  • infoCursor.getInt(infoCursor.getColumnIndex(String.valueOf(ProductSQLiteHelper.PRODUCT_HOT)))
  • infoCursor.getString(infoCursor.getColumnIndex(ProductSQLiteHelper.PRODUCT_PHOTO))

The Cursor will contain just 1 column, the photo column.

I would suggest changing the SELECT clause to "SELECT * FROM phone", this will then create a Cursor with ALL columns from the table.

However, there is an additional flaw with the code in that you are not checking the result of the moveToFirst.

If the Cursor is empty then moveToFirst will return false. As the Cursor will not be after the last any attempt to retrieve data from the Cursor will fail as there is no data.

I would suggest amending your code to :-

        Cursor infoCursor= infodb.rawQuery("SELECT * FROM phone", null);
        ImageView moviePoster = (ImageView) findViewById(R.id.productPhoto);
        while (infoCursor.moveToNext()) {
            if (infoCursor.getInt(infoCursor.getColumnIndex(String.valueOf(ProductSQLiteHelper.PRODUCT_HOT))) == 1) {
                res = this.getResources();
                String posterResourceName = infoCursor.getString(infoCursor.getColumnIndex(ProductSQLiteHelper.PRODUCT_PHOTO));
                Log.v("resource name",posterResourceName);
                int resId = res.getIdentifier(posterResourceName, "drawable", getPackageName());
                moviePoster.setImageResource(resId);
            }
        }

You may have to write code to handle an empty cursor.

MikeT
  • 51,415
  • 16
  • 49
  • 68
  • I have just tried your code.... I found that the problems still there, but BTW thanks for your answering – VINSON YIP Dec 09 '17 at 12:19
  • If you edit your question and add the stack trace from the log then the issue could be analysed. – MikeT Dec 09 '17 at 19:39