错误:null对象引用上的getReadableDatabase()

时间:2015-10-16 04:32:24

标签: java android database sqlite nullpointerexception

执行super.getReadableDatabase()时显示错误:

  

java.lang.NullPointerException:尝试调用虚方法   ' android.database.sqlite.SQLiteDatabase   android.content.Context.openOrCreateDatabase(java.lang.String,int,   android.database.sqlite.SQLiteDatabase $ CursorFactory,   android.database.DatabaseErrorHandler)'在空对象引用上

以下是我的代码:

    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();时出错

1 个答案:

答案 0 :(得分:1)

您在构造函数中传递给Context的{​​{1}}为null。它必须是有效的SQLiteOpenHelper