我得到一個零星的(約30倍)中的NullPointerException這行代碼。我不知道它來自哪裏以及如何預防它。sporadic NullPointerException儘管事實上,該對象不爲空
public void close() {
if (mDb!=null)
mDb.close();
}
mDb是一個SQliteDatabase對象,Close函數在Helper類中實現。
我在做什麼:我想用一個AsyncTask處理該SQliteDatabase中的一個複雜查詢。 這裏是的AsyncTask的代碼:
private class processCursorTask extends AsyncTask<Integer[], Void, Dataset[]> {
private DbHelper mDb;
processCursorTask(DbHelper db) {
super();
mDb=db;
}
@Override
protected Dataset[] doInBackground(Integer... args) {
Dataset[] res=mDb.getCursorFiltered(args);
mDb.close();
return res;
}
protected void onPostExecute(Dataset[] result) {
setListView(result);
}
}
我在這裏創建這個的AsyncTask(從ListFragment):
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new processCursorTask(new DbHelper(this.getActivity()), mBaseDivision, mBaseValue, mSortOrder, mSortDir).execute();
}
03-26 00:44:20.489:E/SqliteDatabaseCpp (6442):sqlite3_close(0x3b2278) 失敗:27 03-26 00:44:20.520:W/dalvikvm(6442):threadid = 12: 線程正在以未捕獲的異常退出(group = 0x40a691f8)03-26 00:44 :20.520:E/AndroidRuntime(6442):致命例外:AsyncTask#2
03-26 00:44:20.520:E/AndroidRuntime(6442): java.lang.RuntimeException:執行時發生錯誤 doInBackground()03-26 00:44:20.520:E/AndroidRuntime(6442) at android.os.AsyncTask $ 3.done(AsyncTask.java:278)
03-26 00:44:20.520:E/AndroidRuntime(6442):at java.util.concurrent.FutureTask $ Sync.innerSetException(FutureTask .java:273) 03-26 00:44:20.520:E/AndroidRuntime(6442):at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-26 00:44:20.520 :E/AndroidRuntime(6442):在 java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:307)
03-26 00:44:20.520:E/AndroidRuntime(6442):at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-26 00:44:20.520:E/AndroidRuntime 6442):at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:208)
03-26 00:44:20.520:E/AndroidRuntime(6442):at java.util.concurrent.ThreadPoolExecutor。 runWorker(ThreadPoolExecutor.java:1076) 03-26 00:44:20.520:E/AndroidRuntime(6442):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:569) 03-26 00 :44:20.520:E/AndroidRuntime(6442):在 java.lang.Thread.run(Thread.java:856)03-26 00:44:20.520: E/AndroidRuntime(6442):Caus編輯:java.lang.NullPointerException
03-26 00:44:20.520:E/AndroidRuntime(6442):在 android.database.sqlite.SQLiteDatabase.closeDatabase(SQLiteDatabase.java:1156) 03-26 00: 44:20.520:E/AndroidRuntime(6442):at android.database.sqlite.SQLiteDatabase.close(SQLiteDatabase.java:1105) 03-26 00:44:20.520:E/AndroidRuntime(6442):at de。 kialelem.trainer.DbHelper.close(DbHelper.java:676)03-26 00:44:20.520:E/AndroidRuntime(6442):at de.kialelem.trainer.ViewContentDatabaseListFragment $ processCursorTask.doInBackground(ViewContentDatabaseListFragment.java:48 ) 03-26 00:44:20.520:E/AndroidRuntime(6442):at de.kialelem.trainer.ViewContentDatabaseListFra gment $ processCursorTask.doInBackground(ViewContentDatabaseListFragment.java:1) 03-26 00:44:20。520:E/AndroidRuntime(6442):at android.os.AsyncTask $ 2.call(AsyncTask.java:264)03-26 00:44:20.520:E/AndroidRuntime(6442):at java.util.concurrent .FutureTask $ Sync.innerRun(FutureTask.java:305)
03-26 00:44:20.520:E/AndroidRuntime(6442):... 5個
難道是因爲沒有正確的同步併發訪問'mDb'實例嗎? – assylias 2012-03-26 11:56:39
我不確定,但是試着讓你的mDb易失 – 2012-03-26 12:00:04