Nullpointer Exception "void android.media.MediaPlayer.setDataSource(android.content.Context, android.net.Uri)"
mplayer
is not initialized. So when you try to invoke the method setDataSource
it gets null pointer exception. Try to initialize mplayer
before invoking that method.
Spidersaw
Updated on June 04, 2022Comments
-
Spidersaw almost 2 years
I'm trying to take a music file from the music folder of the user in a Fragment, 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:
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); } });
OnActivityResult (Don't forgetting that I'm in a fragment)
public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); getActivity(); if ((resultCode == getActivity().RESULT_OK) && (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(); } }
Here's the log:
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:
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)Solved by re-initializing MediaPlayer and setting the URI as a source.
public void mplayergo(Uri soundUri){ try { mplayer.stop(); mplayer.reset(); mplayer.release(); mplayer = MediaPlayer.create(getActivity(), soundUri); // mplayer.start(); } catch (Exception e) { e.printStackTrace(); } }
-
Spidersaw about 8 yearsYes, I had to re-initialize it and now it works, thank you :)