android - SQLiteDatatBase NullPointerException
问题描述
我写了一个方法(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) {
}
}
解决方案
我认为您exception
在verifyUser
.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;
}
你也可以检查你是否helper
在null
里面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();
// ........
}
推荐阅读
- reactjs - NextJS API 路由与 Firebase 云函数
- rasa - NLU训练需要很长时间
- sql - SCD 类型 2 的 SQL 查询
- eclipse - 如何将eclipse切换到深色主题
- node.js - 如何使用布尔 Mongoose Paginate V2 进行正则表达式过滤?
- javascript - 在异步函数中从数组中删除对象
- node.js - 如何在触发 useEffect 挂钩之前等待来自 redux 存储的数据?
- java - Thymeleaf 和 Springboot th:field Null
- amazon-web-services - 关于使用 cURL 和预定义 URL 上传到 S3 的问题
- javascript - 使用 FlatList 显示 API 数据库中的数据