0

In my App, I have a form. In which user puts CNIC first. If his CNIC exists already in SQLite database. User's name and mobile number will automatically be filled in the edittexts (using TextWatcher). But whenever I type a same CNIC, app crashes. If CNIC exists in database, instead of auto filling the name and phone field, the app crashes. I've tried very much but couldn't solve my problem.

Query method :

public List<SGenerateModel> getGeneratedData(String seller_cnic){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT " + TABLE_COL2 + ", " + TABLE_COL3 + " FROM " + TABLE_NAME + " WHERE " + TABLE_COL4 + " =?" ;
    Cursor cursor = db.rawQuery(query, new String[]{"" + seller_cnic});

    List<SGenerateModel> listOfSGModel = new ArrayList<>();
    while(cursor.moveToNext()){
        SGenerateModel sGenerateModel = new SGenerateModel(cursor.getString(1), cursor.getString(2));
        sGenerateModel.setId(cursor.getInt(cursor.getColumnIndex(DatabaseHelper.TABLE_COL1)));
        listOfSGModel.add(sGenerateModel);
    }
    return listOfSGModel;
}

Query Method called :

   public void generate(){

    if (etSname != null)
        selName = (!etSname.getText().toString().equals("")?
                etSname.getText().toString() : "NULL");

    if (etSphone != null)
        selPhone = (!etSphone.getText().toString().equals("")?
                etSphone.getText().toString() : "NULL");

    if (etScnic != null)
        selCnic = (!etScnic.getText().toString().equals("")?
                etScnic.getText().toString() : "NULL");

    sGenerateModelList = databaseHelper.getGeneratedData(selCnic);
    for (SGenerateModel sGenerateModel : sGenerateModelList){
        selName = sGenerateModel.getSellerName() ;
        etSname.setText(selName);
        selPhone = sGenerateModel.getSellerMobile();
        etSphone.setText(selPhone);
    }
}

TextWatcher code :

TextWatcher textWatcher2 = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            generate();
        }
    };

etSname.addTextChangedListener(textWatcher2);
    etSphone.addTextChangedListener(textWatcher2);
    etScnic.addTextChangedListener(textWatcher2);

LogCat Error :

 java.lang.IllegalStateException: Couldn't read row 0, col 2 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
    at android.database.CursorWindow.nativeGetString(Native Method)
    at android.database.CursorWindow.getString(CursorWindow.java:438)
    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
    at com.example.cattleapp.helpers.DatabaseHelper.getGeneratedData(DatabaseHelper.java:218)
    at com.example.cattleapp.activities.MainActivity.generate(MainActivity.java:536)
    at com.example.cattleapp.activities.MainActivity$7.afterTextChanged(MainActivity.java:241)
    at android.widget.TextView.sendAfterTextChanged(TextView.java:9485)
    at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:12080)
    at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1262)
    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:574)
    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:507)
    at com.santalu.maskedittext.MaskTextWatcher.format(MaskTextWatcher.kt:67)
    at com.santalu.maskedittext.MaskTextWatcher.afterTextChanged(MaskTextWatcher.kt:21)
    at android.widget.TextView.sendAfterTextChanged(TextView.java:9485)

1 Answers1

2
String query = "SELECT " + TABLE_COL2 + ", " + TABLE_COL3 + " FROM " + TABLE_NAME + " WHERE " + TABLE_COL4 + " =?" ;

In this line you are fetching only 2 column (index 0 and 1), but while getting you are trying to get column number 2. i.e.

cursor.getString(2)

You can only get column 0 and 1.

Try this:

SGenerateModel sGenerateModel = new SGenerateModel(cursor.getString(0), cursor.getString(1));

EDIT:

Always close your cursor after using it. In your code, after while loop put this line before return listOfSGModel in method getGeneratedData

cursor.close();
Rajat Mehra
  • 1,462
  • 14
  • 19
  • App is still crashing but now logcat error is : android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=939 (# cursors opened by this proc=939)… etSname.setText(selName); on this line of generate() method. –  Jun 10 '19 at 07:31
  • @UsamaSajid then you are storing very large String in data base. Debug the value you get from cursor and verify it. – Rajat Mehra Jun 10 '19 at 07:38
  • I've debugged it, name of five alphabets and phone number is of 12 digits.. I don't think so its a large string –  Jun 10 '19 at 08:25
  • No sir ! App is still crashing. Something is wrong with generate method I think. –  Jun 10 '19 at 12:26