0
public class TimePickerClass  extends Activity{

public TimePicker mTimePicker;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_picker_class);
Button saveTimeButton;
mTimePicker = (TimePicker) findViewById(R.id.timePicker);
mTimePicker.setIs24HourView(true);
saveTimeButton = (Button) findViewById(R.id.next_button);
saveTimeButton.setOnClickListener(v -> {
    getTime();
});

  public String getTime(){
//TODO
int hour;
int minute;
hour = mTimePicker.getHour();
minute = mTimePicker.getMinute();
String message;
message = "Time:" + hour +" : " + minute;
return message;
}

Hello! I have a class in which using TimePicker I get the time chosen by the user. I'm trying to do this:

TimePickerClass time = new TimePickerClass();
String userTime = time.getTime();

But this does not work, the application is down. How then can this be realized? The error is as follows:

FATAL EXCEPTION: main
          Process: com.gfc.glanceclock, PID: 32256
          java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.TimePicker.getHour()' on a null object reference
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Hel.peo
  • 19
  • 5
  • It seems as though your call to `mTimePicker.getHour()` is causing the null pointer exception. But, I might not expect this to be possible, assuming `R.id.timePicker` exists in your view. Does it exist there? – Tim Biegeleisen Aug 21 '18 at 05:54
  • make a setter and getter of the following timePicker and pass the instance to another class and used accordingly. – Ashwani kumar Aug 21 '18 at 06:02

2 Answers2

1

You are trying to create activity by yourself, that's why necessary lifecycle methods are not called and you get NullPointerException.

If you want to get time back from your TimePickerClass activity start it for result, for example:

Intent intent = new Intent(context, TimePickerClass.class)
startActivityForResult(intent, REQUEST_CODE)

and the set result in your TimePickerClass activity:

mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                 Intent intent = new Intent();
                 intent.putExtra("time", time.getTime());
                 setResult(RESULT_OK, intent);
                 finish();
            }
        });

then you can handle result in

protected void onActivityResult(int requestCode, int resultCode, Intent data)

method.

You can read more about it here.

Bracadabra
  • 3,609
  • 3
  • 26
  • 46
0

You Need to use setOnTimeChangedListener() to achieve this:-

As you are trying to call the timer methods by using button click event it's get NullPointerException for the timer.

mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                 getTime();
            }
        });
Mr. Roshan
  • 1,777
  • 13
  • 33