-3

I am following this tutorial for implementing date and time on edit text click.

public class LoadActivity extends AppCompatActivity {

 @SuppressLint("StaticFieldLeak")
 static EditText DateEdit;
  
  @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_load);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
     
     EditText startTimeEditText = (EditText) findViewById(R.id.start_time);   

    startTimeEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showTruitonTimePickerDialog(v);
            showTruitonDatePickerDialog(v);
        }
    });

   


}  // end onCreate



   public void showTruitonDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

public static class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(requireActivity(), this, year, month, day);
    }

  
}

    @SuppressLint("SetTextI18n")
    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        DateEdit.setText(day + ":" + (month + 1) + ":" + year);
    }


}// end main class

I am able to see the date fragment in my app but when I press ok button the app crashes and I am getting error

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference at com.thumbsol.accuratemobileassetsmanagament.LoadActivity$DatePickerFragment.onDateSet(LoadActivity.java:300) at android.app.DatePickerDialog.onClick(DatePickerDialog.java:134)

The line number 300 is DateEdit.setText(day + ":" + (month + 1) + ":" + year);

How can I get rid of this error? Any help would be highly appreciated.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Faisal Qayyum
  • 132
  • 13
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) Don't tag (or reference) the IDE for run-time exceptions. The IDE is not the problem, the code is. – Andrew Thompson Aug 16 '20 at 12:35

2 Answers2

1

You only define static EditText DateEdit, and also need to get it via findViewById. for example:

DateEdit = (EditText) findViewById(R.id.date_edit);
navylover
  • 12,383
  • 5
  • 28
  • 41
0

That's because you're not setting the value of DateEdit anywhere in your code.

Tip: try to not use a view as static. It may cause leaks.

Augusto Carmo
  • 4,386
  • 2
  • 28
  • 61