title: Need an if statement to stop MediaPlayer object only if it's playing
body: <p>I have created a timer, so that when it finishes, music plays. But the song does not ever stop playing if the user presses the back button. So I tried to override the lifecycle method <code>onPause()</code>, which worked, so if the timer had finished and the music was actually playing - then using the back button stopped the music.</p>
  
  <p>However, when a user tries to press the back button of their device <strong>before</strong> the timer finishes (and the music has not started yet), then I get a <code>null pointer exception</code> in the <code>onPause()</code> method.</p>
  
  <p>What can be causing this? How do I write code that says "if MediaPlayer <strong>is</strong> playing, then stop and release it, otherwise do nothing". That is what I need conceptually. (Or there might be a better way to write it?)</p>
  
  <p><strong>Here is my code snippet for the MediaPlayer in the timer's <code>onFinishes()</code> method, which makes the song start playing, when the timer finishes.</strong> (<code>mpCease</code> is just a global variable that represents <code>mp</code>, so I can use it outside of <code>onFinish()</code> ).</p>
  
  <pre><code>public void onFinish() {
                  mTextField.setText("BRAINS!!");
                  // call some media function here
                  MediaPlayer mp = MediaPlayer.create(ZombieThemedActivity.this, R.raw.zombie_sound);
                  mp.start();
                  mpCease = mp;
  
                  // This disables the pause/play button when timer ends
                  Button disablePausePlay = (Button)findViewById(R.id.btnPause);
                  disablePausePlay.setEnabled(false);
          }
  </code></pre>
  
  <p><strong>Here is the onPause method written so far (it's not complete), which works really well to stop the music when the back button is pressed (only if it's already playing).</strong></p>
  
  <pre><code>@Override
      protected void onPause(){
              super.onPause();
          if (mpCease.isPlaying()) {
              mpCease.stop();
              mpCease.release();
          } else if (!mpCease.isPlaying()) {
              // it should do nothing to mpCease or to anything else
          }
      }
  </code></pre>
  
  <p><strong>LogCat</strong> (when back button is pressed <strong>before</strong> timer is finished, it crashes).</p>
  
  <pre><code>FATAL EXCEPTION: main
      java.lang.RuntimeException: Unable to pause activity {com.azurespot.disastertimer.app/com.azurespot.disastertimer.app.themedactivities.ZombieThemedActivity}: java.lang.NullPointerException
              at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2838)
              at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2794)
              at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2772)
              at android.app.ActivityThread.access$800(ActivityThread.java:130)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:4745)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
              at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.NullPointerException
              at com.azurespot.disastertimer.app.themedactivities.ZombieThemedActivity.onPause(ZombieThemedActivity.java:157)
              at android.app.Activity.performPause(Activity.java:5106)
              at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1225)
              at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2825)
              at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2794)
              at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2772)
              at android.app.ActivityThread.access$800(ActivityThread.java:130)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:4745)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>