私は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
を作成する場合、私はこのエラーを修正するにはどうすればよいですか?どのような助けもありがとうございます、事前に感謝します。
は)' ...お返事のための – alfasin
おかげで、私は私の 'updateContact'メソッドに貼り付けています。 –