0
package org.jaberrio.personai2;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;

import java.util.Calendar;

public class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {

@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(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {


    DataBaseManager dataBaseManager = new DataBaseManager();
    String date = String.valueOf(month) + "/" + String.valueOf(day) + "/" + String.valueOf(year);
    dataBaseManager.tempSaveCurrentEvent(DataBaseManager.FieldTypes.DUE_DATE,date, getActivity().getApplicationContext());

    TextView dueDate = (TextView)view.getRootView().findViewById(R.id.dueDate);
    dueDate.setText(dataBaseManager.tempGetCurrentEvent(DataBaseManager.FieldTypes.DUE_DATE,getActivity().getApplicationContext()));
}
}

LOGCAT:

06-23 19:04:05.451  13931-13931/org.jaberrio.personai2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: org.jaberrio.personai2, PID: 13931
java.lang.NullPointerException
        at org.jaberrio.personai2.DatePickerFragment.onDateSet(DatePickerFragment.java:36)
        at android.app.DatePickerDialog.tryNotifyDateSet(DatePickerDialog.java:148)
        at android.app.DatePickerDialog.onClick(DatePickerDialog.java:116)
        at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

I am calling dueDate.setText() however it is returning null. I called view.getRootView() because I am inside the view of DatePicker therefore it should return the view containing my dueDate TextView. I could also pass the View from the method calling DatePickerFragment but I do not know how.I am guessing that DatePicker view when called getRootView() does not return the view that contains R.id.dueDate. Thanks in advance.

Jaberrio
  • 5
  • 4
  • Have you tried to debug your Code? Have you checked which of the variables is `null`? See the thousands of "How to debug a NPE" Threads on SO. – Alexander Jun 23 '15 at 23:29
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Alexander Jun 23 '15 at 23:29
  • I know that dueDate is null, as for some reason TextView dueDate = (TextView)view.getRootView().findViewById(R.id.dueDate); is not setting its value however why this is I do not know as calling view.getRootView().findViewById(R.id.dueDate); should work as it does in other sections of the programs. However I believe the null has something to do with me calling it from Date Picker view. – Jaberrio Jun 23 '15 at 23:40

0 Answers0