1

When i try to click on the button several time then i encountered this error. Below are the error in logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.huiiy.myapplication, PID: 3933
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
        at com.example.huiiy.myapplication.Activity9$13.onClick(Activity9.java:288)
        at android.view.View.performClick(View.java:5712)
        at android.view.View$PerformClick.run(View.java:22514)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6141)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Below are the coding of my application:

    iv_33.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MediaPlayer ivbsound = new MediaPlayer();
            ivbsound = MediaPlayer.create(Activity9.this,R.raw.ivbutton);
            //          MediaPlayer ivbsound = MediaPlayer.create(Activity9.this,R.raw.ivbutton);
            ivbsound.start();
            int theCard = Integer.parseInt((String) view.getTag());
            doStuff(iv_33, theCard);
        }
    });
nkrivenko
  • 1,231
  • 3
  • 14
  • 23
Thomas.Chan
  • 31
  • 2
  • 9
  • But i have initialize the variable suppose not to hit null pointer exception in this point. – Thomas.Chan Nov 02 '20 at 09:59
  • 1
    Look twice: you reassign `ivbsound = MediaPlayer.create()` ... must then return null. That is the thing. Android is tricky, you have to really understand when the framework does what. – GhostCat Nov 02 '20 at 10:28
  • @GhostCat Yes, this is the one cause error, thank you so much buddy! the problem resolved. – Thomas.Chan Nov 02 '20 at 11:25

3 Answers3

3

Try to create player with another audio file. Maybe the ivbutton file is corrupt

[UPDATE]

If you are reusing it declare lazy property of this sound player and on every click play it

class MyActivity extends AppCompatActivity {
    private MediaPlayer soundPlayer = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        soundPlayer = MediaPlayer.create(this, R.raw.ivbutton);

        iv_33.setOnClickListener(view -> {
            soundPlayer.start();
            int theCard = Integer.parseInt((String) view.getTag());
            doStuff(iv_33, theCard);
        });
    }
}
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
  • 1
    Hi thanks for suggestion, but the file is working file since after i click on 8th or 9th time then cause the application crashed and show me this error and before that the sound was played as well. – Thomas.Chan Nov 02 '20 at 09:58
2

It means that the creation of your MediaPlayer failed (maybe you have warn/debug messages about it). It's useless to create a new MediaPlayer and then replace it by a new one.

Check if R.raw.ivbutton exist and correspond to a good media (which can be runned).

Personnally, this is how I do:


    File mediaFile = obj.getFile();
    MediaPlayer mediaPlayer = new MediaPlayer();
    try {
        mediaFile.setReadable(true); // be sure that the program have access to the media content
        
        // this try/catch is to support multiple Android version
        try {
             mediaPlayer.setDataSource(mediaFile.getAbsolutePath());
        } catch (Exception e) {
             mediaPlayer.setDataSource(context, Uri.fromFile(mediaFile));
        }
    
        mediaPlayer.prepare(); // prepare it, after you can change volume ...
    } catch (Exception e){
        e.printStackTrace();
    }
    mediaPlayer.start(); // finally start the media
Elikill58
  • 4,050
  • 24
  • 23
  • 45
1

please make sure that your sound file format you try to play is supported one check list from here. as the MediaPlayer.create returns null in case of the file is not supported which cause you null pointer exception. besides i recommend to check if(ivbsound != null) before calling ivbsound.start();

Ramy Ibrahim
  • 656
  • 4
  • 19