2015-10-16 60 views
1

當執行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();它是錯誤

+0

只需使用'getReadableDatabase()'而不使用'this'。 – Satya

+0

它的工作,謝謝你 – zys

回答

1

Context您在構造函數中傳遞給SQLiteOpenHelper爲null。它需要是有效的Context

+0

哦,我在片段中創建context = getActivity()。謝謝 – zys

+0

在片段附加到任何活動並且getActivity()返回null之前,您要做的太早了。 – laalto

+0

好了現在我只是使用getReadableDatabase()沒有上下文,它的工作 – zys

相關問題