title: Media player null pointer exception
body: <p>I have a really weird error that I just can't figure out what's going on. In my code, I use media player through SoundsClass like this:</p>
  
  <pre><code>public class SoundsClass {
  
  private int sound;
  private Context mContext;
  public SoundsClass(int sound, Context mContext) {
      this.sound=sound;
      this.mContext=mContext;
  }
  
      public void clickOnAndroid() {
  
        switch(this.sound) {
        case R.raw.sound1:
          MediaPlayer mediaPlayer = MediaPlayer.create(mContext, R.raw.sound1);
          mediaPlayer.start();
          mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });
  
        break;
        case R.raw.sound2:
  
         mediaPlayer = MediaPlayer.create(mContext, R.raw.sound2);
         mediaPlayer.start();
       mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
           public void onCompletion(MediaPlayer mp) {
               mp.release();
           }
       });
      break;
      case R.raw.sound3:
         mediaPlayer = MediaPlayer.create(mContext, R.raw.sound3);
         mediaPlayer.start();
         mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });
        break;
        }
  
  
      }
  }
  </code></pre>
  
  <p>So, I use this class in my main activity like this:</p>
  
  <pre><code>    new SoundsClass(R.raw.sound1, getBaseContext()).clickOnAndroid();
  </code></pre>
  
  <p>On 2 places in this activity it works fine, but the third one gives a NullPointerException, caused by line in SoundsClass pointing to:</p>
  
  <pre><code>mediaPlayer.start();
  </code></pre>
  
  <p>Any idea why? Am I using MediaPlayer in a wrong way?</p>
  
  <p>Logcat is:</p>
  
  <pre><code>java.lang.NullPointerException
  at com.testingapp.app.SoundsClass.clickOnAndroid(SoundsClass.java:42)
  at com.testingapp.app.MainActivity$2.onClick(MainActivity.java:147)
  at android.view.View.performClick(View.java:2485)
  at android.view.View$PerformClick.run(View.java:9080)
  at android.os.Handler.handleCallback(Handler.java:587)
  at android.os.Handler.dispatchMessage(Handler.java:92)
  at android.os.Looper.loop(Looper.java:130)
  at android.app.ActivityThread.main(ActivityThread.java:3683)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:507)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Maybe I should add that I call this SoundsClass very often and with none or almost no pause in between. And sometimes I get:</p>
  
  <pre><code>bufferCount is to small and increased to 12.
  </code></pre>