您在从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:14:58.075 28663-28663 / info.androidhive.uplus W / System.err:at 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:at 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:at android.os.Handler.dispatchMessage(Handler.java:102) 08-07 04:14:58.076 28663-28663 / info.androidhive.uplus W / System.err:在android.os.Looper.loop(Looper.java:234) 08-07 04:14:58.076 28663-28663 / info.androidhive.uplus W / System.err:at 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(Native Method) 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)
请问任何人,我希望他/她可以 致谢
答案 0 :(得分:3)
检查参数中的上下文,确保它不为空。
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