-1

I wrote a method(verifyUser) that checks if the user already exists in the database or not , but i get this error:

( java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.example.testapp.DatatBaseHelper.getReadableDatabase()' on a null object reference)

That's my ContentProvider Class:

public class TestProvider extends ContentProvider {

    private DatatBaseHelper helper;
    private SQLiteDatabase db;
    public static final String LOG_TAG = TestProvider.class.getSimpleName();
    private static final int test=1;
    private static final int test_id=2;

    private static final UriMatcher urim=new UriMatcher(UriMatcher.NO_MATCH);

    static{
        urim.addURI(Test.test.authority,Test.test.TABLE_NAME,test);
        urim.addURI(Test.test.authority,Test.test.TABLE_NAME+"/#",test_id);
    }
    @Override
    public boolean onCreate() {
        helper=new DatatBaseHelper(getContext());
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        int match=urim.match(uri);

      db=helper.getReadableDatabase();

        Cursor c;

        switch(match){
            case test:
                c=db.query(Test.test.TABLE_NAME,projection,selection,selectionArgs,null,null,sortOrder);
                break;

            case test_id:
                selection= Test.test.ID+"=?";
                selectionArgs=new String[]{String.valueOf(ContentUris.parseId(uri))};
                c=db.query(Test.test.TABLE_NAME,projection,selection,selectionArgs,null,null,sortOrder);

                break;
            default:
                throw new IllegalStateException("Cannot query form: " + uri);
        }

        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {

         db=helper.getWritableDatabase();

        String email=values.getAsString(Test.test.EMAIL);





        int match=urim.match(uri);

        switch (match){
            case test:
          int   id=(int)(db.insert(Test.test.TABLE_NAME,null,values));




                uri= ContentUris.withAppendedId(uri, id);
        }


        return uri;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }


    public boolean verifyUser(String email){

        SQLiteDatabase db=helper.getReadableDatabase();

        Cursor c;

        String pro[]={Test.test.ID};
        String select=Test.test.EMAIL+"=?";
        String args[]={email};

        c=db.query(Test.test.TABLE_NAME,pro,select,args,null,null,null);

        if(c.getCount()>0){
            return true;
        }

        return false;
    }

That's my datatbase class:

public class DatatBaseHelper extends SQLiteOpenHelper {

    public static final  String DB_NAME="dress.db";
    public static final int DB_VERSION=1;

    public DatatBaseHelper(Context c){
        super(c,DB_NAME,null,DB_VERSION);
    }


    private  String TABLE_CREATE= "CREATE TABLE " + test.TABLE_NAME+ "( "+
  test. ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+ test.NAME+" TEXT NOT NULL,"+
   test. EMAIL+" TEXT NOT NULL,"+ test.PASSWORD+ " TEXT NOT NULL);";



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

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }






    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
lina saeed
  • 81
  • 1
  • 8

1 Answers1

0

From your error it's obviously that helper is null. The reference that you provide in onCreate() method is lost for some reason. To fix your problem and also make your code better in case you will never need more then one instance of DatatBaseHelper I would recommend you to use Singleton pattern for your helper to be sure it will not be null ever.

public class DatatBaseHelper extends SQLiteOpenHelper {

    public static final  String DB_NAME="dress.db";
    public static final int DB_VERSION=1;

    private static DatatBaseHelper instance;

    private DatatBaseHelper(Context c){
        super(c,DB_NAME,null,DB_VERSION);
    }

    public static DatatBaseHelper getInstance(Context c) {
        if(instance == null) {
            instance = new DatatBaseHelper(c);
        }
    }

    private  String TABLE_CREATE= "CREATE TABLE " + test.TABLE_NAME+ "( "+
  test. ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+ test.NAME+" TEXT NOT NULL,"+
   test. EMAIL+" TEXT NOT NULL,"+ test.PASSWORD+ " TEXT NOT NULL);";



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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

And then when you need you helper call it like this.

db = DatatBaseHelper.getInstance().getReadableDatabase();