1

I am creating a function whereby I retrieve contacts via a webservice, but I want to create bookmarked/favorited contacts that are to be added into an SQLite database. After I add the favorite, and return to the activity I need to pass in the contact details to determine whether or not the contact has been marked as a favorite, and if it is, my FloatingActionButton would be of a certain set color. However (since I am using a webservice to retrieve the details first), I am doing this part of the code in the onPostExecute method of my async task. It supposedly passes in to a method in my SQLite Database helper, to check whether that contact's email exists or not, and if it exists, my FloatingActionButton is red, if not it stays my app's primary color.

This is the code in my onPostExecute

    // check whether fav is selected
    boolean found;
    if (tvConEmail.getText().toString() != null) {
        try {
            found = db.findFavByEmail(tvConEmail.getText().toString());
            if(found){
                favSelected = true;
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
            favSelected = false;
        }
    }

However, no matter if the favorite is IN the database or not I catch the NullPointerException. This is my method in the database helper

public boolean findFavByEmail(String email) {
        boolean exists = true;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_FAVORITES,
                COLUMNS,
                "email = ?",
                new String[]{email},
                null, // group by
                null, // having
                null, // order by
                null); // limit

        exists = (cursor.getCount() >0);
        cursor.close();

        return exists;
    }

How can I solve this problem and retrieve the boolean found without a NullPointerException? Or maybe my problem is with the findFavByEmail method which purpose is to check whether there is an existing record with the email passed in as a parameter? I have checked and logged, my email value is not null.

user5808807
  • 71
  • 1
  • 12

0 Answers0