2

I created raw folder inside res directory and inserted mp3 file.I try to play my mp3 file with mediaplayer. this is a my source -

 MediaPlayer mediaPlayer=MediaPlayer.create(MainActivity.this,R.raw.music_1);
 mediaPlayer.start();

but when I run app I have exception.this is a my logcat exception -

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference

How I can solve my problem? If anyone knows solution,please help me. Thanks in advance.

Aditi Parikh
  • 1,522
  • 3
  • 13
  • 34
donoachua
  • 193
  • 2
  • 16

1 Answers1

0

The exception java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference of course means that you are trying to call the start() method on a MediaPlayer object that is null.

This of course means that the call MediaPlayer mediaPlayer=MediaPlayer.create(MainActivity.this,R.raw.music_1); has returned null.

Possible reasons for that are

  • MainActivity.this does not represent a valid Context at the point of your code where you have this call
  • R.raw.music_1 does not actually point to any file
  • The MP3 file in your ...res/raw/ folder is corrupted
  • The MP3 file in your ...res/raw/ folder is not in a supported format
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30