0

I have a two timers:

  • theFirstCountDownTimer;
  • theGraceCountDownTimer;

I have a service that uses these two timers where the theGraceCountDownTimer is nested within theFirstCountdownTimer. On the OnDestroy Method, I want to cancel both timers.

private CountDownTimer theFirstCountDownTimer;
private CountDownTimer theGraceCountDownTimer;

//////////////////////////////////////

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.

    return null;
}

//////////////////////////////////////

@Override
public int onStartCommand(Intent intent, int flags, int startId){

    Log.i("Running", "The Service");

    final SharedPreferences myPref = this.getSharedPreferences("testPref321", Context.MODE_PRIVATE);

    theFirstCountDownTimer = new CountDownTimer(myPref.getInt("seekBar", 10000) * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            String myMilis = Long.toString(millisUntilFinished);
            Log.i("Timer One", "OnTick-" + myMilis);
        }

        @Override
        public void onFinish() {

            Log.i("Timer One", "OnFinish");

            theGraceCountDownTimer = new CountDownTimer(10000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {

                    String myMilis = Long.toString(millisUntilFinished);
                    Log.i("Second Timer!", myMilis);

                }

                @Override
                public void onFinish() {

                    Log.i("Finished!", "Heck Ya!");

                }
            }.start();

        }
    }.start();


    return START_STICKY;
}

//////////////////////////////////////

@Override
public  void onDestroy(){

    theGraceCountDownTimer.cancel();
    //This is the error

    theFirstCountDownTimer.cancel();

    super.onDestroy();
}

Additionally, I know the error is specifically with the line, theGraceCountDownTimer.cancel.

The error states:

Process: com.example.jackson.dmtapp, PID: 22854
java.lang.RuntimeException: Unable to stop service com.example.jackson.dmtapp.TheService@8090f39: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.CountDownTimer.cancel()' on a null object reference
    at android.app.ActivityThread.handleStopService(ActivityThread.java:3479)
    at android.app.ActivityThread.-wrap27(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1638)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6316)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.CountDownTimer.cancel()' on a null object reference
    at com.example.jackson.dmtapp.TheService.onDestroy(TheService.java:93)
    at android.app.ActivityThread.handleStopService(ActivityThread.java:3462)
    at android.app.ActivityThread.-wrap27(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1638) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6316) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 
Jackson Ennis
  • 37
  • 1
  • 7
  • `cancel()` both timers with a check for `null`. Have you tried it ? – ADM May 09 '18 at 03:34
  • I tried that and it worked! I thought that since I had established the countdown variables that they wouldn't be null, but this worked wonderfully. If you post it as an answer I can mark it as right! – Jackson Ennis May 09 '18 at 03:38
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – ADM May 09 '18 at 03:46

1 Answers1

0

You need to call cancel() on both timer. Since you initialize theFirstCountDownTimer inside onStartCommand() so it can not be null. But theGraceCountDownTimer can be null as it initialize after theFirstCountDownTimer finished .

@Override
public  void onDestroy(){
    if(theGraceCountDownTimer!=null)
    theGraceCountDownTimer.cancel();
    theFirstCountDownTimer.cancel();
    super.onDestroy();
}
ADM
  • 20,406
  • 11
  • 52
  • 83