-1

I am creating an android media player where I first get an arraylist of songs in the device and display them to the user using a recyclerview. On click, the position of the song clicked and the songs arraylist is passed to another activity(NowPlayingActivity). In this new activity, I have a connection to a service (PlaybackService) that is supposed to play the song. The connection sets PlaybackService arraylist to the one that’s passed into the NowPlayingActivity. Here is the connection:

private ServiceConnection musicConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        PlaybackService.MusicBinder binder = (PlaybackService.MusicBinder) service;
        playbackService = binder.getService();
        playbackService.setList(myTrackList);
        musicBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        musicBound = false;
    }
};

In the service, I have a setter method to set the track number to be played as follows:

public void setTrack(int trackIndex){
    trackPosition = trackIndex;
}

the method is invoked from NowPlayingActivity as follows:

public void trackPicked(int thisTrack){
    int trackPicked = thisTrack;
    playbackService.setTrack(trackPicked);
    playbackService.playTrack();
}

Calling trackPicked() from the NowPlayingActivity causes a null Pointer Exception with android studio displaying a runtime error as follows:

11-13 12:21:17.915  19688-19688/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.syntaxx.app.euphoria/com.syntaxx.app.euphoria.Activities.NowPlayingActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5371)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.syntaxx.app.euphoria.Activities.NowPlayingActivity.trackPicked(NowPlayingActivity.java:49)
            at com.syntaxx.app.euphoria.Activities.NowPlayingActivity.onCreate(NowPlayingActivity.java:42)
            at android.app.Activity.performCreate(Activity.java:5122)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5371)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

what could be causing the exception?

Abhinav singh
  • 1,448
  • 1
  • 14
  • 31
yadda
  • 21
  • 3
  • @Selvin , chill out man. Just seeking some insight, not criticism. Thanks for the first reference though – yadda Nov 13 '15 at 13:18

1 Answers1

0

you might want to look at this:

What is a stack trace, and how can I use it to debug my application errors?

It will teach you how to use your stack trace to debug the program on your own.

But basically, you need to look at this line of code and figure out what object is missing or null:

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
Community
  • 1
  • 1
Misha
  • 3
  • 3