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?