title: Android : MediaPlayer getDuration() and getCurrentPosition() has NullPointerException
body: <p>Creating a <code>MediaPlayer</code>.
  The particular class has the functions to play, pause and resume. I am trying to make a <code>SeekBar</code> which will display the progress. 
  <code>SeekBar</code> requires as the value of the <code>maxlength</code> which is to be obtained from the <code>getDuration()</code> function which calls the <code>mediaplayer.getDuration()</code> function and returns the value.
  <code>SeekBar</code> also requires the value of the current position which is obtained from the <code>getCurrentPostion()</code> function.
  Both of them are returning a <code>NullPointerException</code>.</p>
  
  <pre><code>public class PlayMedia extends AsyncTask&lt;Void, Void, Void&gt;
      implements MediaPlayer.OnPreparedListener {
  
  
  private MediaPlayer mediaPlayer;
  int length;
  int duration =0;
  int currentPosition=0;
  @Override
  protected Void doInBackground(Void... params) {
      mediaPlayer = new MediaPlayer();
      try {
          mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
          mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/Notate/"+MainActivity.filepath+".wav");
          mediaPlayer.setOnPreparedListener(this);
          mediaPlayer.prepareAsync();
      } catch (IllegalArgumentException e) {
          e.printStackTrace();
      } catch (IllegalStateException e) {
          e.printStackTrace();
      } catch (IOException e) {
  
      }
      return null;
  }
  
  @Override
  protected void onPostExecute(Void result) {
      // TODO Auto-generated method stub
      super.onPostExecute(result);
  
     }
  public int getDuration(){
      duration=mediaPlayer.getDuration();
      Log.d("Position",Integer.toString(duration));
      return duration;
  }
  public int getPresentDuration(){
      currentPosition=mediaPlayer.getCurrentPosition();
      Log.d("CurrentPosition",Integer.toString(currentPosition));
      return currentPosition;
  }
  
  @Override
  public void onPrepared(MediaPlayer mp) {
      // TODO Auto-generated method stub
      if(mediaPlayer != null &amp;&amp; mediaPlayer.isPlaying()){
          mediaPlayer.pause();
      }
      else if(mediaPlayer != null){
      mediaPlayer.start();
          }
      Log.d("Shivram", "Started Playing");
  
  }
  public void pause(){
      mediaPlayer.pause();
      length=mediaPlayer.getCurrentPosition();
      Log.d("Shivram","Paused");
  }
  public void resume(){
      mediaPlayer.seekTo(length);
      mediaPlayer.start();
      Log.d("Shivram","Resume");
  </code></pre>
  
  <p>This Fragment Class calls the <code>getDuration()</code> function, where the exception is created.</p>
  
  <pre><code>public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      contentViewC = inflater.inflate(R.layout.fragment_fragment_b, container, false);
      final SeekBar seekBar;
  
      Button button = (Button) contentViewC.findViewById(R.id.button1);
      Button button2 = (Button) contentViewC.findViewById(R.id.button2);
  
      textToChange=(TextView)contentViewC.findViewById(R.id.textView1);
      final PlayMedia play=new PlayMedia();
      boolean pause=false;
      seekBar = (SeekBar) contentViewC.findViewById(R.id.SeekBar01);
      final int[] recordingDuration = new int[1];
  
  
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (MainActivity.filepath != "Nothing") {
                  if(play.getStatus()==AsyncTask.Status.FINISHED || play.getStatus()==AsyncTask.Status.RUNNING){
                      Log.d("Shivram-FragmentA", "Resume");
                      play.resume();
  
                  }
                  if(play.getStatus()==AsyncTask.Status.PENDING) {
                      play.execute();
                      recordingDuration[0] =play.getDuration();
                      seekBar.setMax(recordingDuration[0]);
                  }
                  Frequency getFreq=new Frequency();
  
                getFreq.execute();
              }
          }
      });
  </code></pre>
  
  <p>The stackTrace is as follows</p>
  
  <pre><code>01-13 21:44:15.182  16263-16263/com.example.shivram.notate E/AndroidRuntime﹕ FATAL EXCEPTION: main
  Process: com.example.shivram.notate, PID: 16263
  java.lang.NullPointerException
          at com.example.shivram.notate.PlayMedia.getDuration(PlayMedia.java:43)
          at com.example.shivram.notate.FragmentC$1.onClick(FragmentC.java:66)
          at android.view.View.performClick(View.java:4456)
          at android.view.View$PerformClick.run(View.java:18465)
          at android.os.Handler.handleCallback(Handler.java:733)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:136)
          at android.app.ActivityThread.main(ActivityThread.java:5086)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:515)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
          at dalvik.system.NativeStart.main(Native Method)
  </code></pre>