title: I need to cancel the Timer started in a service class from MainActivity, but get null reference exception
body: <p>I have a service type class which starts a timertask in onCreate method and I need to stop the timer from MainActivity when user press the button. I know I have to keep the reference to the timer in my service, but I cannot figure out how to do that and need some help, please!</p>
  
  <p>please take a look at my code </p>
  
  <pre><code>package com.example.timertest;
  
  import android.content.Intent;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  
  public class MainActivity extends AppCompatActivity implements View.OnClickListener {
      Button button;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          button = findViewById(R.id.button);
          button.setOnClickListener(this);
          startService(new Intent(this, TimeService.class));
  
      }
  
      @Override
      public void onClick(View v) {
          if(v.getId() == R.id.button){
              // i need here to call mTimer.cancel() in TimeService.class
              //
          }
      }
  }
  </code></pre>
  
  <p>// and here is the TimeService.java</p>
  
  <pre><code>package com.example.timertest;
  
  import android.app.Service;
  import android.content.Intent;
  import android.os.Handler;
  import android.os.IBinder;
  import android.support.annotation.Nullable;
  import android.widget.Toast;
  
  
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.Timer;
  import java.util.TimerTask;
  
  public class TimeService extends Service {
      public Timer mTimer;
      private Handler mHandler = new Handler();
      long NOTIFY_INTERVAL = 60 * 1000 * 1; // 1 min
  
      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      @Override
      public void onCreate() {
          if (mTimer != null) {
              mTimer.cancel();
          } else {
              mTimer = new Timer();
          }
          mTimer.scheduleAtFixedRate(new RefreshDataTimerTask(), 0, NOTIFY_INTERVAL);
      }
  
      class RefreshDataTimerTask extends TimerTask {
  
          @Override
          public void run() {
              mHandler.post(new Runnable() {
  
                  @Override
                  public void run() {
                      Toast.makeText(getApplicationContext(), getDateTime(), Toast.LENGTH_LONG).show();
                  }
              });
          }
  
          private String getDateTime() {
              SimpleDateFormat sdf = new SimpleDateFormat("[yyyy/MM/dd - HH:mm:ss]");
              return sdf.format(new Date());
          }
      }
  }
  
  </code></pre>
  
  <p>//and service registered in manifest</p>
  
  <pre><code>&lt;service android:name=".TimeService"/&gt;
  </code></pre>
  
  <p>I have tried to call the mTimer.cancel(), but got a null reference, because seems I have created a new instance of the service class.</p>
  
  <p>This example shows a Toast with date and time each minute, i want when for example 15 seconds have past and i press the button, the timer to be canceled and to start counting 60 seconds again from the beginning. Inside this service i have many other things like notifications,channels, shared preferences and so on, so it will be good if i can operate only with the timer object.</p>