lundi 14 septembre 2015

Return a string if row id doesnt exist

Here i have method to get the status of a person based on its id_number/number from the table. If person number exist it will return its status like PENDING or ACCEPTED.

However it number does not exist it should return string of NONE instead. The code works only if number exist but it returns ArrayIndexOutOfBounds if number does not exist;

How can i prevent ArrayIndexOutOfBounds so that if the number does not exist it will return to a value of string NONE.

   public String getStatus(int number) {
     SQLiteDatabase db = this.getWritableDatabase();

     Cursor cursor = db.query(TABLE_NAME, new Sting[] {KEY_NUMBER,
              KEY_FULLNAME, KEY_STATUS}, KEY_NUMBER + "=?", 
               new String[] {String.valueOf(number)}, null, null, null, null);

         if(cursor != null) {
           cursor.moveToFirst();

           String status = cursor.getString(cursor.getColumnIndex("remark");
           return status;
         }
    }

i call the method something like this;

   getStatus(100); 

if this number doesnt exist it will crash and logcat shows ArrayIndexOutOfBounds

Aucun commentaire:

Enregistrer un commentaire