嗨,我在从Web服务器获取数据并将其直接插入SQLite数据库时遇到问题,但是首先我需要检查数据是否在SQLite中已经不是它们的问题了。
java.lang.NullPointerException:尝试调用虚拟方法'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String,int,android.database.s
我的代码如下:
下面是用于获取数据的后台类的代码,我尝试仅指定将返回的数据添加到SQLite的方法
@Override
protected void onPostExecute(String result) {
try {
DbHelper dbHelper=new DbHelper(ctx);
boolean test;
for (int j = 0; j < data.size(); j++) {
Log.i("group", data.get(j));
if(dbHelper.checkGroupId(DataId.get(j))==false)
{
//now data can be added
test=dbHelper.saveToLocalDatabase(DataId.get(j),data.get(j),grpName.get(j),"no");
if(test)
{
Toast.makeText(ctx,"Data Added "+data.get(j),Toast.LENGTH_LONG).show();
//notify gui
showNotification();
}
else
{
Toast.makeText(ctx,"Data not Added",Toast.LENGTH_LONG).show();
}
}
}
dbHelper.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
和以下是将数据添加到SQLite数据库的代码
private static final int DATABASE_VERSION=1;
public static final String CREATE_TABLE="create table if not exists groups(groupId varchar(50),groupName text,targetAmount text,fetched varchar(10))";
public static final String DROP_TABLE="drop table if exists "+DbContract.TABLE_NAME;
public DbHelper(Context context)
{
super(context,DbContract.DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE);
onCreate(db);
}
public boolean saveToLocalDatabase(String groupId,String groupName,String targetAmount,String fetched)
{
SQLiteDatabase database=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(DbContract.GROUP_ID,groupId);
contentValues.put(DbContract.GROUP_NAME,groupName);
contentValues.put(DbContract.TARGET_AMOUNT,targetAmount);
contentValues.put(DbContract.FETCHED,fetched);
//now add data to databse
long result=database.insert(DbContract.TABLE_NAME,null,contentValues);
if(result==-1)
{
return false;
}
else
{
return true;
}
}
此函数检查数据库中是否还没有数据,这是显示错误来自backgroundTask类的位置
//function to check if the given id is available
public boolean checkGroupId(String groupId)
{
String query="select * from groups where groupId='"+groupId+"'";
SQLiteDatabase database=this.getWritableDatabase();;
//create cursor to return data
Cursor cursor=database.rawQuery(query,null);
int counter=0;
while(cursor.moveToNext())
{
counter=counter+1;
}
//check counters returned
if(counter>0)
{
return true;
}
else
{
return false;
}
}
我的输出终端日志数据中的错误所在
08-07 04:14:58.075 28663-28663/info.androidhive.uplus W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
08-07 04:14:58.075 28663-28663 / info.androidhive.uplus W / System.err:at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
08-07 04:14:58.075 28663-28663 / info.androidhive.uplus W / System.err:位于android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
08-07 04:14:58.075 28663-28663 / info.androidhive.uplus W / System.err:at info.androidhive.uplus.DbHelper.checkGroupId(DbHelper.java:165)
08-07 04:14:58.075 28663-28663 / info.androidhive.uplus W / System.err:at info.androidhive.uplus.BackgroundTask.onPostExecute(BackgroundTask.java:136)
08-07 04:14:58.075 28663-28663 / info.androidhive.uplus W / System.err:at info.androidhive.uplus.BackgroundTask.onPostExecute(BackgroundTask.java:28)
08-07 04:14:58.075 28663-28663 / info.androidhive.uplus W / System.err:位于android.os.AsyncTask.finish(AsyncTask.java:651)
08-07 04:14:58.075 28663-28663 / info.androidhive.uplus W / System.err:at android.os.AsyncTask.access $ 500(AsyncTask.java:180)
08-07 04:14:58.075 28663-28663 / info.androidhive.uplus W / System.err:at android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:668)
08-07 04:14:58.075 28663-28663 / info.androidhive.uplus W / System.err:位于android.os.Handler.dispatchMessage(Handler.java:102)
08-07 04:14:58.076 28663-28663 / info.androidhive.uplus W / System.err:at android.os.Looper.loop(Looper.java:234)
08-07 04:14:58.076 28663-28663 / info.androidhive.uplus W / System.err:位于android.app.ActivityThread.main(ActivityThread.java:5526)
08-07 04:14:58.076 28663-28663 / info.androidhive.uplus W / System.err:at java.lang.reflect.Method.invoke(本机方法)
08-07 04:14:58.076 28663-28663 / info.androidhive.uplus W / System.err:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
08-07 04:14:58.076 28663-28663 / info.androidhive.uplus W / System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
请任何我希望他/她可以的人
谢谢
最佳答案
在params中检查Context,确保它不为null。
java.lang.NullPointerException:尝试调用虚拟方法
'android.database.sqlite.SQLiteDatabase
android.content.Context.openOrCreateDatabase(java.lang.String,int,
android.database.s
上面的错误是因为您的上下文为空。
DbHelper dbHelper=new DbHelper(ctx);// ctx should not be null