0

When I click Submit Button in Change password class or layout the app crashes. What's wrong with my code, please teach me! Thanks in Advance. I am working with Sqlite database for user login system and user can change their password from app bar layout. On clicking >>change password in App Bar new activity pops up i.e changePassword Activity. Upto here everything works fine but after typing password and clicking on submit button the app crashes and sets back to MainActivity.

Here is my change password class

public class changePasswordActivity extends AppCompatActivity {

EditText oldpasswordEditText;
EditText newpasswordEditText;
EditText confirmpasswordEditText;
Button btnsubmit;

String realusername, realpassword;


String checkoldpass, checknewpass, checkconfirmpass;

SQLiteHelper sqLiteHelper;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_change_password);

    oldpasswordEditText = findViewById(R.id.oldpasswordEditText);
    newpasswordEditText = findViewById(R.id.newPasswordEditText);
    confirmpasswordEditText = findViewById(R.id.confirmPasswordEditText);
    btnsubmit= findViewById(R.id.btnsubmit);

    checkoldpass = oldpasswordEditText.getText().toString();
    checknewpass = newpasswordEditText.getText().toString();
    checkconfirmpass = confirmpasswordEditText.getText().toString();

    realusername = getIntent().getStringExtra("USERNAME");
    realpassword= getIntent().getStringExtra("PASSWORD");

    btnsubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(!(checkconfirmpass.equals(checknewpass))) {
                Toast.makeText(changePasswordActivity.this,"New Password didn't matched",Toast.LENGTH_SHORT).show();

            }else if (!(realpassword.equals(checkoldpass))){
                Toast.makeText(changePasswordActivity.this,"Old password didn't matched",Toast.LENGTH_SHORT).show();
            }
            else if (checkconfirmpass.equals(checkoldpass)){
                Toast.makeText(changePasswordActivity.this,"New password cannot be same as old password",Toast.LENGTH_SHORT).show();

            }else if(checkconfirmpass.equals(checknewpass.equals(checkoldpass))) {


                Toast.makeText(changePasswordActivity.this, "New password cannot be same as old password", Toast.LENGTH_SHORT).show();
            }
            else{
                sqLiteHelper = new SQLiteHelper(changePasswordActivity.this);
                sqLiteHelper.changepassword(checknewpass,realusername);
            }

        }
    });
}
}

Here is my SQLiteHelper class

public class SQLiteHelper extends SQLiteOpenHelper {

SQLiteDatabase db;
private static final String DATABASE_NAME = "info.db";
private static final int DATABASE_VERSION = 1;

public static final String TABLE_NAME = "profile";
public static final String COLUMN_ID = "userid";
public static final String COLUMN_FULLNAME = "fullname";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_PASSWORD = "password";
public static final String COLUMN_MOBILE = "mobile";

private static final String CREATE_TABLE_QUERY =
        "CREATE TABLE " + TABLE_NAME + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_FULLNAME + " TEXT, " +
                COLUMN_EMAIL + " TEXT, " +
                COLUMN_PASSWORD + " TEXT, " +
                COLUMN_MOBILE + " TEXT " + ")";

//modified constructor
public SQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE_QUERY);
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}


public Cursor getData() {
    String query = "SELECT * FROM" + TABLE_NAME;

    Cursor data = db.rawQuery(query, null);
    return data;
}

public void changepassword(String mpassword, String mname) {



   Cursor cur= db.rawQuery("UPDATE "+SQLiteHelper.TABLE_NAME+" SET "
           +SQLiteHelper.COLUMN_PASSWORD
           +" = '"+mpassword+"' WHERE "+ SQLiteHelper.COLUMN_EMAIL+" = ?",new String[]{mname});

}
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Sushan Bastola
  • 473
  • 6
  • 10

1 Answers1

2

db will be null, there is also no need for a Cursor, so change

public void changepassword(String mpassword, String mname) {
   Cursor cur= db.rawQuery("UPDATE "+SQLiteHelper.TABLE_NAME+" SET "
           +SQLiteHelper.COLUMN_PASSWORD
           +" = '"+mpassword+"' WHERE "+ SQLiteHelper.COLUMN_EMAIL+" = ?",new String[]{mname});
}

To :-

public void changepassword(String mpassword, String mname) {
   SQLiteDatabase db = this.getWriteableDatabae();
   db.rawQuery("UPDATE "+SQLiteHelper.TABLE_NAME+" SET "
           +SQLiteHelper.COLUMN_PASSWORD
           +" = '"+mpassword+"' WHERE "+ SQLiteHelper.COLUMN_EMAIL+" = ?",new String[]{mname});
}

You will also have the same issue with the getData method so you should add SQLiteDatabase db = this.getWriteableDatabae(); to the getData method as well.


Alternately you could change :-

public SQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

to :-

public SQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    db = this.getWriteableDatabase();
}

In which case db will be a valid SQLiteDatabase.

MikeT
  • 51,415
  • 16
  • 49
  • 68