2017-06-18 67 views
-1

我創建一個SQLiteOpenHelper的SQL數據庫,我可以更新完成的數據。它顯示java.lang.NullPointerException當我創建另一個類對話框

這是關於更新DatabaseHandler.java

//Updating single contact 
public int updateContact(Contact contact) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_DATE, contact.getDate()); 
    values.put(KEY_BMORNING, contact.getBeforeMorning()); 
    values.put(KEY_AMORNING, contact.getAfterMorning()); 
    values.put(KEY_BNOON, contact.getBeforeNoon()); 
    values.put(KEY_ANOON, contact.getAfterNoon()); 
    values.put(KEY_BNIGHT, contact.getBeforeNight()); 
    values.put(KEY_ANIGHT, contact.getAfterNight()); 

    //updating row 
    Log.d("update:", String.valueOf(contact.getID())); 
    return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?", 
      new String[] {String.valueOf(contact.getID())}); 
} 

我的代碼更新數據,它的工作原理:

//Contact is my jave bean class 
db.updateContact(new Contact(id, choiceDate, choiceValue, "", "", "", "", "")); 

我想另一個類使用對話,所以我創造這樣的:

public class DialogHandler extends AppCompatActivity{ 

    private DatabaseHandler db = new DatabaseHandler(this); 
    private Context context; 

    public DialogHandler(Context context){ 
     this.context = context; 
    } 

    // Alert dialog 
    public void updateDialog(String rightMessage, String leftMessage, final Contact contact) { 
     android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context); 
     builder.setMessage("Data is existed") 
       .setPositiveButton(rightMessage, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 
         db.updateContact(contact); 
        } 
       }); 
     builder.setNegativeButton(leftMessage, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 

      } 
     }); 
     builder.show(); 
    } 
} 

然後我改變這樣的更新代碼:

new DialogHandler(this).updateDialog("Update", "Cancel", new Contact(id, choiceDate, choiceValue, "", "", "", "", "")); 

它顯示了我的錯誤: 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

,並在此代碼SQLiteDatabase db = this.getWritableDatabase();

atcom.example.motogod19.testmysql1.DatabaseHandler.updateContact(DatabaseHandler.java:200) 

,如果我爲顯示對話框創建DialogHandler.java如何解決這個問題?任何幫助將不勝感激,在此先感謝。

+0

你忘了我們展示裏面'getWritableDatabase相關的代碼()'... – alfasin

+0

感謝您的回覆,我有它貼在我的'updateContact'方法。 –

回答

1

您的DatabaseHandler需要一個上下文,儘管您只能在構造函數中獲取上下文。您的IDE沒有給出警告,因爲您將DialogHandler擴展爲AppCompatActivity,這使得它有缺陷。

private DatabaseHandler db = null; 
    private Context context; 

    public DialogHandler(Context context){ 
     this.context = context; 
     db = new DatabaseHandler(context); 
    } 
+0

感謝您的幫助,我通過您的建議解決了問題,再次感謝。 –

+0

歡迎您:) – Enzokie

相關問題