0

I am trying to initialise an intent in a function like below :

public void playNextOver() {
    songInfo detail = (songInfo) songAdapter.getItem(globalPos);

    songTitle = detail.getTitle();

    songPath = detail.getPath();
    songId = String.valueOf(detail.getID());
    songArtist = detail.getArtist();

    Intent globalIntentTemp = new Intent(getApplicationContext(), songPlay.class);
    globalIntentTemp.putExtra("SONG_ID", songId);
    globalIntentTemp.putExtra("SONG_TITLE", songTitle);
    globalIntentTemp.putExtra("SONG_ARTIST", songArtist);
    globalIntentTemp.putExtra("SONG_PATH", songPath);
    getApplicationContext().startActivity(globalIntentTemp);
}

I am doing this inside an activity.

But it is giving me following error at runtime on this statement

Intent globalIntentTemp = new Intent(getApplicationContext(), songPlay.class); 

09-06 00:32:19.441 25790-25790/com.example.coderahul.beats E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.coderahul.beats, PID: 25790 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106) at com.example.coderahul.beats.playlistManager.playNextOver(playlistManager.java:503) at com.example.coderahul.beats.songPlay$2.onClick(songPlay.java:703) at android.view.View.performClick(View.java:4848) at android.view.View$PerformClick.run(View.java:20299) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:218) at android.app.ActivityThread.main(ActivityThread.java:5657) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:990) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:785)

I don't know what to do since I am new to Android. Please help.

  • 2
    Please edit your question and post the entire activity and the entire Java stack trace. – CommonsWare Sep 05 '16 at 18:48
  • Question is updated please have a look. – coderahul1994 Sep 05 '16 at 18:55
  • it still does not have information about the state in which method is called – Nayan Srivastava Sep 05 '16 at 18:59
  • Please post your function `getAppContext()` – hqt Sep 05 '16 at 18:59
  • Actually it is getApplicationContext() instead of getAppContext() and thanks @hqt for understanding :) – coderahul1994 Sep 05 '16 at 19:04
  • 1
    Before complaining about the behavior of others, focus first on your own behavior. I asked you to post the entire activity. You decided not to do that. As a result, others decided to give you a generic response, since giving you specific help is difficult without a [mcve]. If I had to guess, `playlistManager` is not a valid activity. For example, perhaps you created an instance of `playlistManager` via its constructor, which will result in an invalid activity. – CommonsWare Sep 05 '16 at 19:24

0 Answers0