當執行super.getReadableDatabase()
顯示錯誤:錯誤:getReadableDatabase()上的空對象引用
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
下面是我的代碼:
public synchronized SQLiteDatabase getReadableDatabase() {
return super.getReadableDatabase();
}
public List<AllCaseItem> getOtherPersonalCase(int personid) {
Cursor c = null;
List<AllCaseItem> myList = new ArrayList<AllCaseItem>();
String selection="SELECT " + TABLE_CaseFolder + "." + CaseFolder_CaseId + "," +
TABLE_CaseFolder + "." + CaseFolder_CaseCoverImageFileName + "," +
TABLE_CaseFolder + "." + CaseFolder_CaseName + "," +
TABLE_CaseFolder + "." + CaseFolder_CaseColor + "," +
"COUNT(*)," + TABLE_CaseFolder + "." + CaseFolder_IsMedicalCase + "," +
TABLE_CaseFolder + "." + CaseFolder_StartDate + "," +
TABLE_CaseFolder + "." + CaseFolder_EndDate +
" FROM " + TABLE_Event + " LEFT JOIN " + TABLE_CaseFolder +
" ON " + TABLE_CaseFolder + "." + CaseFolder_CaseId + " = " + TABLE_Event + "." + CaseFolder_CaseId +
" WHERE " + TABLE_Event + "." + Person_PersonId + " = " + personid +
" AND " + TABLE_Event + "." + Event_IsDeleted + " = 0 " +
" AND " + TABLE_CaseFolder + "." + CaseFolder_IsDeleted + " = 0" +
" AND " + TABLE_CaseFolder + "." + Person_PersonId + " = 0" +
" GROUP BY " + TABLE_Event + "." + CaseFolder_CaseId;
try {
synchronized (DBLOCK) {
SQLiteDatabase db = this.getReadableDatabase();
c = db.rawQuery(selection, null);
if (c != null && c.moveToFirst()) {
do {
AllCaseItem allCaseItem = new AllCaseItem();
allCaseItem.setCaseId(c.getInt(c.getColumnIndex(CaseFolder_CaseId)));
allCaseItem.setImageName(c.getString(c.getColumnIndex(CaseFolder_CaseCoverImageFileName)));
allCaseItem.setColor(c.getString(c.getColumnIndex(CaseFolder_CaseColor)));
allCaseItem.setCaseType(c.getString(c.getColumnIndex(CaseFolder_CaseName)));
allCaseItem.setStartDate(c.getLong(c.getColumnIndex(CaseFolder_StartDate)));
allCaseItem.setEndDate(c.getLong(c.getColumnIndex(CaseFolder_EndDate)));
allCaseItem.setIsMedicalCase(c.getInt(c.getColumnIndex(CaseFolder_IsMedicalCase)) == 1);
myList.add(allCaseItem);
} while (c.moveToNext());
}
}
c.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return myList;
}
當執行SQLiteDatabase db = this.getReadableDatabase();
它是錯誤
只需使用'getReadableDatabase()'而不使用'this'。 – Satya
它的工作,謝謝你 – zys