1

I have a lot of edittext to show a DatePickerDialog, so I make a method and edittext as a parameter. Then Java said parameter should be final when it accessed from within inner class.

But then when I run the app, it stopped at edittext.settext() I suspect edittext as a final is the problem because I have another code that doesn't make edittext as parameter run well.

this is the code :

     public DatePickerDialog setDateTanggalBeri(final EditText editTanggal){

    DatePickerDialog datePickTgl;
    Calendar newCalendar = Calendar.getInstance();

    datePickTgl = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
            final Calendar newDate = Calendar.getInstance();
            newDate.set(i,i1,i2);
            editTanggal.setText(dateFormatter.format(newDate.getTime()));
        }
    },newCalendar.get(Calendar.YEAR),newCalendar.get(Calendar.MONTH),newCalendar.get(Calendar.DAY_OF_MONTH));

    return datePickTgl;

}

and here is the error :

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.droid.anindya.jadwalimunisasibayiku, PID: 5669
              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
                  at com.droid.anindya.jadwalimunisasibayiku.Display_Jadwal$1.onDateSet(Display_Jadwal.java:209)
                  at android.app.DatePickerDialog.onClick(DatePickerDialog.java:137)
                  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

and this is how I implemented :

state outside onCreate:

DatePickerDialog DPD1;
DatePickerDialog DPD2;
DatePickerDialog DPD3;
DatePickerDialog DPD4;
DatePickerDialog DPD5; 

state inside onCreate:

DPD1 = setDateTanggalBeri(textTgl1);
DPD2 = setDateTanggalBeri(textTgl2);
DPD3 = setDateTanggalBeri(textTgl3);
DPD4 = setDateTanggalBeri(textTgl4);
DPD5 = setDateTanggalBeri(textTgl5);
....

editText on clicked :

public void tanggalBeri1Clicked(View view) {
        DPD1.show();
}
public void tanggalBeri2Clicked(View view) {
    DPD2.show();
}

....

2 Answers2

3

Since you said that EditText is in the same Activity, I recommend making it a class private object.

Example:

public class MainActivity extends AppCompatActivity {

    private EditText editTanggal; //declaration

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ...
        //Initiate the EditText here
        //Example:
        //editTanggal = (EditText) findViewById(R.id.editText);
        ...
    }

    ...

    public DatePickerDialog setDateTanggalBeri() {

        DatePickerDialog datePickTgl;
        Calendar newCalendar = Calendar.getInstance();

        datePickTgl = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                final Calendar newDate = Calendar.getInstance();
                newDate.set(i, i1, i2);
                editTanggal.setText(dateFormatter.format(newDate.getTime())); //Only use
            }
        }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

        return datePickTgl;
    }
}

Doing this way, the EditText does not have to be final and the setDateTanggalBeri() method can work in the same way.

I hope it helps.

Duan Bressan
  • 627
  • 6
  • 13
  • but I have to make editText as a parameter so I can call the method for more than one edittext. I have 30 editText to show datepickerdialog. – Anindya Dev Mar 02 '17 at 20:44
  • I understand your problem, let me read the documentation here to remember, but how do you create a **class that extends EditText** and implement its method in there and use it, `editTanggal.setDateTanggalBeri()` – Duan Bressan Mar 02 '17 at 20:49
  • I add how I use the method – Anindya Dev Mar 02 '17 at 21:21
0

eventually, I solve the problem inspired from nullPointerException document. Thank all of you guys so I have clue to discover the issue. Previously, I declare editText at 2 places, outside the oncreate and again inside method. It turned out, editText inside method redeclare and makes it null or cannot be reached by another method.

To be more clearly, here are before and after solution :

before :

EditText textTgl1;
EditText textTgl2;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table_jadwal);
    ......
    setTanggalBeri()

    DPD1 = setDateTanggalBeri(textTgl1);
    DPD2 = setDateTanggalBeri(textTgl2);
    ....
}
public void setTanggalBeri(){
  EditText textTgl1 = (EditText) findViewById(R.id.editTanggal1);
  EditText textTgl2= (EditText) findViewById(R.id.editTanggal2);
}

after :

EditText textTgl1;
EditText textTgl2;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table_jadwal);
    ......
    setTanggalBeri()

    DPD1 = setDateTanggalBeri(textTgl1);
    DPD2 = setDateTanggalBeri(textTgl2);
    ....
}
public void setTanggalBeri(){
    textTgl1 = (EditText) findViewById(R.id.editTanggal1);
    textTgl2 = (EditText) findViewById(R.id.editTanggal2);
 }