if else gives fatal error

I want to check whether entered username already existed in my database or not. if not then add up to database. I made function which gives correct result if username existed. The problem is when i entered a name which does not exist in my database the apps stops. When i entered a name which existed in database it Toast me. Kindly tell me why apps get stopped.

I select database with this query,

// Check if user already exist or not
    public Cursor isAlreadyExist(String name){
        Cursor cursor = null;
        SQLiteDatabase db = this.getWritableDatabase();
        cursor = db.rawQuery("SELECT " + NAME_USER + " FROM " + TABLE_USER
                + " WHERE " + NAME_USER + "=" + name, null);
        return cursor;
    } 

Function to get username from database,

// Check if user already exist
     public String isAlreadyExist(String name) {
         Cursor cursor = db.isAlreadyExist(name);
         cursor.moveToFirst();
         String names = cursor.getString(cursor.getColumnIndex(db.NAME_USER));
        return names;
    }

using of this function,

username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);

        login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                    String name = username.getText().toString();
                    if(name.equals(isAlreadyExist(name))){
                        //db.addNewUser(username.getText().toString(), util.computeSHAHash(password.getText().toString()));
                        Toast.makeText(MainActivity.this, "already", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(MainActivity.this, "User not exist", Toast.LENGTH_LONG).show();
                    }

Upvotes: 0

Views: 84

Answers (2)

Manish Menaria
Manish Menaria

Reputation: 27431

1.Check cursor is null or not before using moveToFirst() function

2.Initilize the String

Write this function

public String isAlreadyExist(String name) {
     Cursor cursor = db.isAlreadyExist(name);
     cursor.moveToFirst();
     String names = cursor.getString(cursor.getColumnIndex(db.NAME_USER));
    return names;
}

as

 public String isAlreadyExist(String name) {
     Cursor cursor = db.isAlreadyExist(name);
     String names="";
     if(cursor!=null){
        cursor.moveToFirst();
     names = cursor.getString(cursor.getColumnIndex(db.NAME_USER));
     }else{
        names = "User does not exist";
     }
   return names;
}

Upvotes: 0

Kumar M
Kumar M

Reputation: 1014

The problem is with the cursor.moveToFirst(); when cursor is empty, need to check the cursor size if it is greater than zero then we need to moveToFirst. Update this method and check.

Function to get username from database,

public String isAlreadyExist(String name)
    {
        String names = "";
        Cursor cursor = db.isAlreadyExist(name);
        if (cursor.getCount() > 0)
        {
            cursor.moveToFirst();
            names = cursor.getString(cursor.getColumnIndex(db.NAME_USER));
        }

        return names;
    }

Upvotes: 2

Related Questions