2017-06-18 1 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

は)' ...お返事のための – alfasin

+0

おかげで、私は私の 'updateContact'メソッドに貼り付けています。 –

答えて

1

DatabaseHandlerにはコンストラクタのコンテキストしか取得できませんが、コンテキストが必要です。 DialogHandlerAppCompatActivityに拡張しているため、IDEに警告が表示されませんでした。あなたは私たちに `getWritableDatabase(内部の関連するコードを表示するのを忘れ

private DatabaseHandler db = null; 
    private Context context; 

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

あなたの助けてくれてありがとう、私はあなたの提案で問題を解決しました、もう一度ありがとう。 –

+0

あなたは歓迎です:) – Enzokie

関連する問題