title: Cursor Error On Android Studio
body: <p>I have some problem when try to make a ListView that contain data from database which i carry using the cursor.
  But when i run the activity, the error report gimme this error message :</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.</p>
  </blockquote>
  
  <p>I don't know what happen, i have searching the answer on StackOverFlow i found some answer that i must assign the value of Cursor, but it still didn't solve my error.
  Please, can somebody help me with easy understanding words.. 
  Thanks before.</p>
  
  <p>NB. This my code for <code>CategorySetting.java</code> (an <code>Activity</code> that contain the <code>ListView</code>)</p>
  
  <pre><code>public class CategorySetting extends Activity {
      private SQLiteDatabase db;
      private CustomCursorAdapter customAdapter ;
      private static Button BtnIAddCateg;
      private static Button BtnICancelCateg;
      private static final String TAG = CategorySetting.class.getSimpleName();
      DatabaseHelper dBHelper = new DatabaseHelper (this);
      private ListView list;
      private ArrayList&lt;String&gt; arrCategId = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; arrCategName = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; arrCategNote = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; arrCategCurr = new ArrayList&lt;String&gt;();
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_category_setting);
          onButtonClickButtonListener();
          //ListView list = getListView();
          //showListView();
          displayData();
      }
  
      public void showListView(){
          list = (ListView)findViewById(android.R.id.list);
          list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) {
                  Log.d(TAG, "clicked on item: " + arg2);
                  String label = arg0.getItemAtPosition(arg2).toString();
                  Toast.makeText(CategorySetting.this, "You Selected " + label, Toast.LENGTH_LONG).show();
                  //click to update data
                  Intent i = new Intent(getApplicationContext(), AddCategory.class);
                  //i.putExtra("CategId", arrCategId.get(arg2));
                  i.putExtra("CategName", arrCategName.get(arg2));
                  i.putExtra("CategNote", arrCategNote.get(arg2));
                  i.putExtra("CategCurr", arrCategCurr.get(arg2));
                  i.putExtra("update", true);
                  startActivity(i);
  
  
              }
          });
  
          new Handler().post(new Runnable() {
              @Override
              public void run() {
                  customAdapter = new CustomCursorAdapter(CategorySetting.this, dBHelper.getAllData());
                  list.setAdapter(customAdapter);
              }
          });
      }
  
      private void displayData() {
          db = dBHelper.getReadableDatabase();
          Cursor mCursor = db.rawQuery("SELECT * FROM " + dBHelper.TABLE_Categ_NAME, null);
  
          arrCategId.clear();
          arrCategName.clear();
          arrCategNote.clear();
          arrCategCurr.clear();
          if (mCursor.moveToFirst()) {
              do {
                  arrCategId.add(mCursor.getString(mCursor.getColumnIndex(dBHelper.COL1)));// error disini make sure cursor is bla bla
                  arrCategName.add(mCursor.getString(mCursor.getColumnIndex(dBHelper.COL2)));
                  arrCategNote.add(mCursor.getString(mCursor.getColumnIndex(dBHelper.COL3)));
                  arrCategCurr.add(mCursor.getString(mCursor.getColumnIndex(dBHelper.COL4)));
  
              } while (mCursor.moveToNext());
          }
          DisplayAdapter disadpt = new DisplayAdapter(CategorySetting.this, arrCategId, arrCategName, arrCategId, arrCategCurr);
          list.setAdapter(disadpt);
          mCursor.close();
      }
          //ListView view = getListView();
          //iew.addHeaderView(getLayoutInflater().inflate(R.layout.row, null));
          //db = dBHelper.getWritableDatabase();
          //this.muat_ulang();
  
  
  
      /*public void reload(){
          try {
              DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
              db = dbHelper.getWritableDatabase();
              Cursor c = db.rawQuery("SELECT CategName FROM " + tableName, null);
              if (c != null ) {
                  if  (c.moveToFirst()) {
                      do {
                          String categName = c.getString(c.getColumnIndex("CategName"));
                      }while (c.moveToNext());
                  }
              }
          } catch (SQLiteException se ) {
              Log.e(getClass().getSimpleName(), "Could not create or Open the database");
          } finally {
              if (db != null)
                  db.execSQL("DELETE FROM " + tableName);
              db.close();
          }
  
      }*/
  
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_category_setting, menu);
          return true;
      }
  
      public void onButtonClickButtonListener(){
  
          BtnIAddCateg = (Button)findViewById(R.id.btnAddNewCateg);
          BtnIAddCateg.setOnClickListener(
                  new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          Intent intentAddCateg = new Intent ("com.example.ever_ncn.cashflow.AddCategory");
                          startActivity(intentAddCateg);
                      }
                  }
          );
  
          BtnICancelCateg = (Button)findViewById(R.id.btnCancelCateg);
          BtnICancelCateg.setOnClickListener(
                  new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          Intent intent = new Intent(
                                  CategorySetting.this,
                                  MainActivity.class
                          );
                          startActivity(intent);
                      }
                  }
          );
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>
  
  <p>And this one for <code>DatabaseHelper.java</code></p>
  
  <pre><code>public class DatabaseHelper extends SQLiteOpenHelper {
  
      public static final String MyVillageSoftware = "MyVillageSoftware";
      public static final String DATABASE_NAME = "Cashflow.db";
      public static final String TABLE_Categ_NAME = "category_table";
      public static final String TABLE_Trans_NAME = "transaction_table";
      public static final String COL1 = "CategId";
      public static final String COL2 = "CategName";
      public static final String COL3 = "Note";
      public static final String COL4 = "Currency";
      public static final String COL5 = "_id";
      //TOL for transaction Coloumn
      public static final String TOL1 = "TransId";
      public static final String TOL2 = "TransName";
      public static final String TOL3 = "Amount";
      public static final String TOL4 = "TransNote";
      public static final String TOL5 = "TransDate";
      public static final String TOL6 = "CategId";
      public static final String TOL7 = "_id";
      private static final String TAG = DatabaseHelper.class.getSimpleName();
  
  
  
      public DatabaseHelper(Context context) {
          super(context, DATABASE_NAME, null, 9);
  
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
  
          db.execSQL("Create table " + TABLE_Categ_NAME +
                  " (CategID Integer PRIMARY KEY AUTOINCREMENT, " +
                  "CategName Text," +
                  " Note Text," +
                  " Currency Text," +
                  " _id Text)");
  
          db.execSQL("Create table " + TABLE_Trans_NAME +
                  " (TransID Integer PRIMARY KEY AUTOINCREMENT, " +
                  "TransName Text," +
                  " Amount Integer," +
                  " TransNote Text," +
                  " TransDate long," +
                  " CategId Text, " +
                  " _id Text)");
      }
  
      public boolean insertCategData(String categname, String note, String currency, String id){
          SQLiteDatabase db = this.getWritableDatabase();
          ContentValues contentValues = new ContentValues();
          contentValues.put(COL2, categname);
          contentValues.put(COL3, note);
          contentValues.put(COL4, currency);
          contentValues.put(COL5, id);
          long result = db.insert(TABLE_Categ_NAME, null, contentValues);
           if (result == -1)
               return false;
           else
               return true;
  
      }
  
      public boolean insertTransData (String transname, Integer amount, String transnote, long transdate, String categ, String id){
          SQLiteDatabase db = this.getWritableDatabase();
          ContentValues contentValues = new ContentValues();
          //contentValues.put(TOL1, transname);
          contentValues.put(TOL2, transname);
          contentValues.put(TOL3, amount);
          contentValues.put(TOL4, transnote);
          contentValues.put(TOL5, transdate);
          contentValues.put(TOL6, categ);
          contentValues.put(TOL7, id);
          long result = db.insert(TABLE_Trans_NAME, null, contentValues);
          if (result == -1)
              return false;
          else
              return true;
  
      }
  
  
      public List&lt;String&gt; getAllCategory() {
          List&lt;String&gt; AllCategoryList = new ArrayList&lt;String&gt;();
  
              String selectQuery = "SELECT * FROM " + TABLE_Categ_NAME;
              SQLiteDatabase db = this.getReadableDatabase();
              Cursor cursor = db.rawQuery(selectQuery, null);
  
              /*db = SQLiteDatabase.openDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
              Cursor allrows = db.rawQuery("SELECT * FROM " + TABLE_Categ_NAME, null);
              System.out.println("COUNT : " + allrows.getCount());*/
  
              if (cursor.moveToFirst()) {
                  do {
  
                      String ID = cursor.getString(0);
                      String Categ = cursor.getString(1);
                      String Note = cursor.getString(2);
                      String Curr = cursor.getString(3);
                      AllCategoryList.add(Categ);
  
                  } while (cursor.moveToNext());
              }
              cursor.close();
              db.close();
              return AllCategoryList;
          }
      public Cursor getAllData() {
          SQLiteDatabase db = this.getReadableDatabase();
  
          String buildSQL = "SELECT * FROM " + TABLE_Categ_NAME;
  
          Log.d(TAG, "getAllData SQL: " + buildSQL);
  
          return db.rawQuery(buildSQL, null);
      }
  
  
  
      /*public ArrayList&lt;String&gt;getAllCategory(){
          ArrayList&lt;String&gt; AllCategoryList = new ArrayList&lt;String&gt;();
          SQLiteDatabase db = this.getReadableDatabase();
          String selectCateg="Select * FROM " +TABLE_Categ_NAME;
          Cursor cursor = db.rawQuery(selectCateg, null);
          if(cursor.getCount()&gt;0){
              while (cursor.moveToNext()){
                  String categname1=cursor.getString(cursor.getColumnIndex(COL2));
                  AllCategoryList.add(COL2);
  
              }return AllCategoryList;
          }
  
          return AllCategoryList;
  
      }*/
  
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_Categ_NAME);
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_Trans_NAME);
          onCreate(db);
  
      }
  }
  </code></pre>
  
  <p>this one for the <code>DisplayAdapter.java</code></p>
  
  <pre><code>public class DisplayAdapter extends BaseAdapter {
  
      private Context mContext;
      private ArrayList&lt;String&gt; categArrId;
      private ArrayList&lt;String&gt; categArrName;
      private ArrayList&lt;String&gt; categArrNote;
      private ArrayList&lt;String&gt; categArrCurr;
  
      public DisplayAdapter(Context c, ArrayList&lt;String&gt; categId,ArrayList&lt;String&gt; categName, ArrayList&lt;String&gt; categNote, ArrayList&lt;String&gt; categCurr) {
          this.mContext = c;
  
          this.categArrId = categId;
          this.categArrName = categName;
          this.categArrNote = categNote;
          this.categArrCurr = categCurr;
      }
  
      public int getCount() {
          // TODO Auto-generated method stub
          return categArrId.size();
      }
  
      public Object getItem(int position) {
          // TODO Auto-generated method stub
          return null;
      }
  
      public long getItemId(int position) {
          // TODO Auto-generated method stub
          return 0;
      }
  
      public View getView(int pos, View child, ViewGroup parent) {
          Holder mHolder;
          LayoutInflater layoutInflater;
          if (child == null) {
              layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              child = layoutInflater.inflate(R.layout.categsetlist, null);
              mHolder = new Holder();
              mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
              mHolder.txt_name = (TextView) child.findViewById(R.id.txt_categnamelist);
              mHolder.txt_note = (TextView) child.findViewById(R.id.txt_categnotelist);
              mHolder.txt_curr = (TextView) child.findViewById(R.id.txt_categcurrlist);
              child.setTag(mHolder);
          } else {
              mHolder = (Holder) child.getTag();
          }
  
          mHolder.txt_name.setText(categArrName.get(pos));
          return child;
      }
  
      public class Holder {
          TextView txt_id;
          TextView txt_name;
          TextView txt_note;
          TextView txt_curr;
      }
  }
  </code></pre>