smtnkc
smtnkc

Reputation: 508

Android Media Player returns NullPointerException

I know there are similar questions around, but all the answers were specific to those applications. I am getting a nullpointerexception but can't understand the problem. It is a very long activity class so i couldn't share. Sorry if it is not a clear question but all I need is some hints or ideas, how can I find the cause of this exception. LOGCAT:

12-25 10:54:31.845: E/MediaPlayer(14465): error (1, -2147483648)
12-25 10:54:31.865: E/AndroidRuntime(14465): FATAL EXCEPTION: main
12-25 10:54:31.865: E/AndroidRuntime(14465): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sametenekeci.vmail/com.st.vmail.MainActivity}: java.lang.NullPointerException
12-25 10:54:31.865: E/AndroidRuntime(14465):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
12-25 10:54:31.865: E/AndroidRuntime(14465):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
12-25 10:54:31.865: E/AndroidRuntime(14465):    at android.app.ActivityThread.access$600(ActivityThread.java:132)
12-25 10:54:31.865: E/AndroidRuntime(14465):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
12-25 10:54:31.865: E/AndroidRuntime(14465):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-25 10:54:31.865: E/AndroidRuntime(14465):    at android.os.Looper.loop(Looper.java:137)
12-25 10:54:31.865: E/AndroidRuntime(14465):    at android.app.ActivityThread.main(ActivityThread.java:4575)
12-25 10:54:31.865: E/AndroidRuntime(14465):    at java.lang.reflect.Method.invokeNative(Native Method)
12-25 10:54:31.865: E/AndroidRuntime(14465):    at java.lang.reflect.Method.invoke(Method.java:511)
12-25 10:54:31.865: E/AndroidRuntime(14465):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
12-25 10:54:31.865: E/AndroidRuntime(14465):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
12-25 10:54:31.865: E/AndroidRuntime(14465):    at dalvik.system.NativeStart.main(Native Method)
12-25 10:54:31.865: E/AndroidRuntime(14465): Caused by: java.lang.NullPointerException
12-25 10:54:31.865: E/AndroidRuntime(14465):    at com.sametenekeci.vmail.MainActivity.onCreate(MainActivity.java:90)
12-25 10:54:31.865: E/AndroidRuntime(14465):    at android.app.Activity.performCreate(Activity.java:4465)
12-25 10:54:31.865: E/AndroidRuntime(14465):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-25 10:54:31.865: E/AndroidRuntime(14465):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
12-25 10:54:31.865: E/AndroidRuntime(14465):    ... 11 more

Thanks for help.

ADDITION: MAINACTIVITY IS HERE

Upvotes: 1

Views: 3782

Answers (5)

Melquiades
Melquiades

Reputation: 8598

MediaPlayer is a state machine - if you call its' methods in the wrong order, you get problems. There are also two ways to initialise player:

  1. Using create() method - in our case:

    mp = MediaPlayer.create(this, R.raw.entermail);
    

This line will initialise you player with the resource sound passed as an argument. If successful, prepare() will already have been called and must not be called again.

You're getting an error on this line:

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

which indicates that mp is null. Check the result of create() before setting listener:

if (mp != null) {
    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    // rest of the code
}
else {
    Log.d("AUDIO", "MediaPlayer.create() failed. mp is null");
    // deal with it accordingly
}

In your case, check the file you're using in create(), and make sure that (form the Docs):

In this case, a "raw" resource is a file that the system does not try to parse in any particular way. However, the content of this resource should not be raw audio. It should be a properly encoded and formatted media file in one of the supported formats.

  1. You can create MediaPlayer object with the constructor:

    mp = new MediaPlayer();
    

then remember to set data source and prepare:

    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setDataSource(getApplicationContext(), myUri);
    mediaPlayer.prepare();

    //when ready
    mediaPlayer.start();

Remember to catch exceptions on setDataSource. Also remember to call release() on MediaPlayer object when you're done with it.

By the way, after you set your listener, you call setAppState(), which in turn calls:

mp.reset();
mp.setDataSource();
mp.prepare();
mp.start();

So you might as well initialise your player with the second method, and set OnErrorListener to check if there are any errors.

The guide also advises to call any long operations (such as prepare()) on a separate thread, as not to block UI thread. Please go through the guide and read about the states, it will help you realize what's going on.

Upvotes: 2

sergannn
sergannn

Reputation: 11

Hello you can solve your problem by adding <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in your manifest

Upvotes: 1

Hamad
Hamad

Reputation: 5152

your line number 90 causes nullpointer exception,

line number 90: mp.setOnCompletionListener(new OnCompletionListener() {

and cause of nullPointerException is,you haven't initialized media player 1st: //first initialize it

MediaPlayer mp= new MediaPlayer();

then use it,as you want, like this:

mp= new MediaPlayer();
mp = MediaPlayer.create(this, R.raw.entermail);
    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

Upvotes: 2

Co-Gia
Co-Gia

Reputation: 103

I know why ( maybe :) ) In the method setAppState() u call mp.prepare() in the try catch block then u call mp.start() outside that try catch. mp.prepare() may caused an exception.

Call mp.start(); in the try catch block to ensure that mp has prepared successfully.

try {
    mp.prepare();
    mp.start();
} catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Upvotes: 1

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

Change it:

mp.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                // Rest of Code
            }
        });

Upvotes: 2

Related Questions