title: Nullpointer Exception "void android.media.MediaPlayer.setDataSource(android.content.Context, android.net.Uri)"
body: <p>I'm trying to take a music file from the music folder of the user in a <strong>Fragment</strong>, but when I do that and after receiving it in onActivityResult (and logging it), I get a Nullpointer Exception when I want to play it with media player.
  The code to pick the file:</p>
  
  <pre><code>    Button pickFile = (Button) v.findViewById(R.id.btnpick);
      pickFile.setOnClickListener(new OnClickListener() {
  
     @Override
          public void onClick(View v) {
          Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
          startActivityForResult(intent, 1);
          }
      });
  </code></pre>
  
  <p>OnActivityResult (Don't forgetting that I'm in a fragment)</p>
  
  <pre><code>  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data); 
    getActivity();
    if ((resultCode == getActivity().RESULT_OK) &amp;&amp; (data != null)) {
    Log.d("result", "okreceived");
    Uri soundUri = Uri.parse(data.getData().toString());
    Log.i("result", "Intent data " + data.getData().toString());
    mplayergo(soundUri);
         }
    if (resultCode == getActivity().RESULT_CANCELED)
    {
    //null
    }
          }
  
         public void mplayergo(Uri soundUri){
             try {
                 mplayer.setDataSource(getActivity(), soundUri);
                 mplayer.start();
             } catch (IOException e) {
                 e.printStackTrace();
  
             } catch (Exception e) {
                 e.printStackTrace();
             }
  
          }
  </code></pre>
  
  <p>Here's the log:</p>
  
  <blockquote>
    <p>D/result: okreceived I/result: Intent data
    content://media/external/audio/media/34839 W/System.err:
    java.lang.NullPointerException: Attempt to invoke virtual method 'void
    android.media.MediaPlayer.setDataSource(android.content.Context,
    android.net.Uri)' on a null object reference at
    com.package.example.GameFragment.mplayergo(GameFragment.java:764)  at
    com.package.example.GameFragment.onActivityResult(GameFragment.java:748)
    W/System.err:     at
    android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:156)
    W/System.err:     at
    android.app.Activity.dispatchActivityResult(Activity.java:6290)
    W/System.err:     at
    android.app.ActivityThread.deliverResults(ActivityThread.java:3606)
    W/System.err:     at
    android.app.ActivityThread.handleSendResult(ActivityThread.java:3653)
    W/System.err:     at
    android.app.ActivityThread.access$1400(ActivityThread.java:154)
    W/System.err:     at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1371)
    W/System.err:     at
    android.os.Handler.dispatchMessage(Handler.java:102) W/System.err:<br>
    at android.os.Looper.loop(Looper.java:135) W/System.err:     at
    android.app.ActivityThread.main(ActivityThread.java:5300)
    W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
    W/System.err:     at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    W/System.err:     at
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)</p>
  </blockquote>
  
  <p>Solved by re-initializing MediaPlayer and setting the URI as a source.</p>
  
  <pre><code> public void mplayergo(Uri soundUri){
         try {
             mplayer.stop();
             mplayer.reset();
             mplayer.release();
             mplayer = MediaPlayer.create(getActivity(), soundUri);
            // mplayer.start();
         } catch (Exception e) {
             e.printStackTrace();
         }
      }
  </code></pre>