JavaJavaJava
JavaJavaJava

Reputation: 67

FATAL EXCEPTION: main Caused by: java.lang.NullPointerException at playAudio(extras.getInt(MEDIA));

I'm attempting to create an MP3 player inside my app however when I select an MP3 using a file browser, then open it using my app - I get a force close error due to a null pointer exception on the line:

playAudio(extras.getInt(MEDIA));

How might this be avoided?

JAVA:

public class MediaPlayerDemo extends Activity {

    private static final String TAG = "MediaPlayerDemo";
    private MediaPlayer mMediaPlayer;
    private static final String MEDIA = "media";
    private static final int LOCAL_AUDIO = 1;
    private static final int STREAM_AUDIO = 2;
    private static final int RESOURCES_AUDIO = 3;
    private static final int LOCAL_VIDEO = 4;
    private static final int STREAM_VIDEO = 5;
    private String path;

    private TextView tx;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        tx = new TextView(this);
        setContentView(tx);
        Bundle extras = getIntent().getExtras();
        playAudio(extras.getInt(MEDIA));
    }

    private void playAudio(Integer media) {
        try {
            switch (media) {
                case LOCAL_AUDIO:
                    /**
                     * TODO: Set the path variable to a local audio file path.
                     */
                    path = "";
                    if (path == "") {
                        // Tell the user to provide an audio file URL.
                        Toast
                                .makeText(
                                        MediaPlayerDemo.this,
                                        "Please edit MediaPlayer_Audio Activity, "
                                                + "and set the path variable to your audio file path."
                                                + " Your audio file must be stored on sdcard.",
                                        Toast.LENGTH_LONG).show();

                    }
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setDataSource(path);
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();
                    break;
                case RESOURCES_AUDIO:
                    /**
                     * TODO: Upload a audio file to res/raw folder and provide
                     * its resid in MediaPlayer.create() method.
                     */
                    mMediaPlayer = MediaPlayer.create(this, R.raw.test_cbr);
                    mMediaPlayer.start();

            }
            tx.setText("Playing audio...");

        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // TODO Auto-generated method stub
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }

    }
}




12-17 15:55:09.377: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:09.385: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:09.390: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:09.393: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:16.234: I/fengjun1010(5461): --onServiceStateChanged,state=SIM1 0 home MetroPCS T-Mobile 310260  EDGE CSS not supported -1 -1 RoamInd=-1 DefRoamInd=-1 EmergOnly=false Regist state: 1
12-17 15:55:20.395: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:20.395: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:20.395: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:20.397: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:20.397: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:20.398: I/fengjun1010(5461): -----agoldPhoneStateListener-----
12-17 15:55:20.398: I/fengjun1010(5461): ----------mPhoneListenerFlag=true
12-17 15:55:20.437: D/AndroidRuntime(5461): Shutting down VM
12-17 15:55:20.437: W/dalvikvm(5461): threadid=1: thread exiting with uncaught exception (group=0x412ca9a8)
12-17 15:55:20.446: E/AndroidRuntime(5461): FATAL EXCEPTION: main
12-17 15:55:20.446: E/AndroidRuntime(5461): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project/com.example.project.ui.phone.MediaPlayerDemo}: java.lang.NullPointerException
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.ActivityThread.access$600(ActivityThread.java:156)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.os.Looper.loop(Looper.java:153)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.ActivityThread.main(ActivityThread.java:5297)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at java.lang.reflect.Method.invokeNative(Native Method)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at java.lang.reflect.Method.invoke(Method.java:511)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at dalvik.system.NativeStart.main(Native Method)
12-17 15:55:20.446: E/AndroidRuntime(5461): Caused by: java.lang.NullPointerException
12-17 15:55:20.446: E/AndroidRuntime(5461):     at com.example.project.ui.phone.MediaPlayerDemo.onCreate(MediaPlayerDemo.java:31)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.Activity.performCreate(Activity.java:5262)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
12-17 15:55:20.446: E/AndroidRuntime(5461):     ... 11 more

Upvotes: 0

Views: 548

Answers (1)

James McCracken
James McCracken

Reputation: 15766

Most likely culprit is extras.getInt(MEDIA). If extras does not actually contain an object with key MEDIA then it will return null.

You should first check that extras contains the key. Something like this:

if( extras.containsKey( MEDIA ) ) {
    playAudio(extras.getInt(MEDIA);
} else {
    Toast.makeText(this, "No media provided in intent", Toast.LENGTH_SHORT).show();
}

Upvotes: 1

Related Questions