-1

I am working on a music player, but I keep on getting this error whenever I try to play a song

LogCat:

Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
        at com.musicapp.android.musicapp.PlayingScreen.playSongs(PlayingScreen.java:119)

Code:

public void playSongs() {
    if(PlayingScreen_List != null){
        uri = Uri.parse(PlayingScreen_List.get(position).getPath());
    }

    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
    } else {
        mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
    }
    Toast.makeText(getApplicationContext(),String.valueOf(uri),Toast.LENGTH_SHORT).show();
119    mediaPlayer.start();
}

AND no, my uri is not null.....I did a TOAST and it showed me the path for every song I clicked on.....WELPP? :)

"PlayingScreen_List" is not null I did a toast...it looks like this [1]: https://i.stack.imgur.com/iU1jf.jpg

2 Answers2

0

https://developer.android.com/reference/android/media/MediaPlayer#create(android.content.Context,%20android.net.Uri)

Returns MediaPlayer a MediaPlayer object, or null if creation failed

0

Maybe check your PlayingScreen_List?

If

if(PlayingScreen_List != null){
    uri = Uri.parse(PlayingScreen_List.get(position).getPath());
}

is giving you null or not being called that could be causing an error when you use

mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);