SQLiteDatatBase NullPointerException

问题描述 投票:0回答:1

我编写了一个方法(verifyUser),该方法检查用户是否已经存在于数据库中,但出现此错误:

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

这是我的ContentProvider类:

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;
    }
}

这是我的数据库类:

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) {

    }
}
android
1个回答
0
投票

我认为您在exceptionverifyUser方法中获得了此TestProvider

尝试如下修改verifyUser

public boolean verifyUser(String email){
    if(helper == null){
        helper = new DatatBaseHelper(context) // initialize your helper here.
    }

    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;
}

还可以检查helpernull还是不在query内,如

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

        if(helper == null){
           helper = new DatatBaseHelper(context) // initialize your helper.
        }
        db=helper.getReadableDatabase();
        // ........
}
© www.soinside.com 2019 - 2024. All rights reserved.