0

I have now two functions for checking if email exists and if student exists in my sqlite code. But when i call the functions from the register page, the app crashes and the log says that the error is at the isUserinCourse function and the if condition at the register page. I am sharing the code of my functions and registration page. Please tell me if i am making some mistake. I have already tried different combinations of for loops on my registration page. Doesn't work. However if i put only the Email exists function, it works then.

SQLITE code

public boolean ifUserInCourse(String sid) {
        boolean rv = false;
        String whereclause = KEY_SID + "=?";
        String[] whereargs = new String[]{sid};
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor csr = db.query(TABLE_USERINFO,null, whereclause, whereargs,null,null,null);
        if (csr.getCount() > 0) rv = true;
        csr.close();
        return rv;
    }


    public boolean isEmailExists(String email) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_USERS,// Selecting Table
                new String[]{KEY_ID, KEY_EMAIL, KEY_SID, KEY_PASSWORD},//Selecting columns want to query
                KEY_EMAIL + "=?",
                new String[]{email},//Where clause
                null, null, null);

        if (cursor != null && cursor.moveToFirst()&& cursor.getCount()>0) {
            //if cursor has value then in user database there is user associated with this given email so return true
            return true;
        }

        //if email does not exist return false
        return false;
    }

RegisterActivity code

buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (validate()) {
                    String Email = editTextEmail.getText().toString();
                    String SID = editTextSID.getText().toString();
                    String Password = editTextPassword.getText().toString();
                    progressBar.setVisibility(View.VISIBLE);

                    //Check in the database is there any user associated with  this email

                        if (!sqliteHelper.ifUserInCourse(SID) && !sqliteHelper.isEmailExists(Email)) {

                            sqliteHelper.addUser(new User(null, Email, SID, Password));
                            Snackbar.make(buttonRegister, "User created successfully! Please Login ", Snackbar.LENGTH_LONG).show();
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    finish();
                                }
                            }, Snackbar.LENGTH_LONG);


                        } else {
                            if(sqliteHelper.isEmailExists(Email)) {

                                Snackbar.make(buttonRegister, "Email already exists ", Snackbar.LENGTH_LONG).show();


                            } else {

                                Snackbar.make(buttonRegister, "User doesn't exists ", Snackbar.LENGTH_LONG).show();
                            }

                        }

                    progressBar.setVisibility(View.GONE);


                }
            }
        });
  • Doesn't the above not adhere to *I want to create a function where only the student id stored in table_userinfo can register,* that is should you be using `if (sqliteHelper.ifUserInCourse(SID) && !sqliteHelper.isEmailExists(Email)) {` (! removed). P.S. you should close the cursor in the **isEmailExists** method before returning. – MikeT Feb 05 '19 at 19:12
  • Additionally `cursor != null && cursor.moveToFirst()&& cursor.getCount()>0` could simply be `cursor.getCount()>0`. A cursor returned from the **query** will never be null, so that check is useless. The result of **moveToFirst** is little different from checking the number of rows in the cursor, getCount is more indicative of your intention (i.e. you want to know if there is a row, rather than actually access data from the first row). – MikeT Feb 05 '19 at 19:19
  • I was just trying different combinations for calling the functions. It's just not working. It keeps on giving me an error at line 135 that is at db.query. I don't see anything wrong there, there is no error. – Prince Persia Feb 06 '19 at 02:30
  • You need to look at the stack-trace, perhaps refer to [Debug your app](https://developer.android.com/studio/debug/). I suspect that the cause may be due to **TABLE_USERINFO** not referring to an existing table or perhaps **KEY_SID** not being a column within that table. However, without the stack-trace the cause can only be a guess. – MikeT Feb 06 '19 at 06:15
  • Finally i sorted it out. I redid the whole sql code, mostly using yours. I can't exactly say where the error was but i suspect it was in my authenticate function, where i didn't closed the cursor. Also there were some mistakes in my Login Activity. Anyways, thank you for taking time and helping me out. – Prince Persia Feb 06 '19 at 15:39

0 Answers0