11
public class AlarmTask implements Runnable{
// The date selected for the alarm
private final Calendar date;
// The android system alarm manager
private final AlarmManager am;
// Your context to retrieve the alarm manager from
private final Context context;

public AlarmTask(Context context, Calendar date) {

    this.context = context;
    this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); //error for this line
    this.date = date;
}

There are similar questions, but after banging my head I couldn't sort out the problem. I am getting these following errors! Is it due to fragments?

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
        at android.content.ContextWrapper.getSystemService(ContextWrapper.java:562)
        at lol.com.epl.AlarmTask.<init>(AlarmTask.java:24)
        at lol.com.epl.ScheduleService.setAlarm(ScheduleService.java:47)
        at lol.com.epl.ScheduleClient.setAlarmForNotification(ScheduleClient.java:58)
        at lol.com.epl.FixFragment.onCreateView(FixFragment.java:88)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1026)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        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:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

This is how I am calling the class

 public void setAlarm(Calendar c) {
    // This starts a new thread to set the alarm
    // You want to push off your tasks onto a new thread to free up the UI to carry on responding
    new AlarmTask(this, c).run();
}

This is where I call the setAlarm function

  public void setAlarmForNotification(Calendar c){
    ScheduleService mBoundService = new ScheduleService();
    mBoundService.setAlarm(c);
         }
1
  • Your context is null. Post how are you calling this class and where Commented Oct 11, 2015 at 18:26

3 Answers 3

15

In your code the

this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

the context is null.

Checking your stacktrace your are initializing the AlarmTask in

at lol.com.epl.FixFragment.onCreateView(FixFragment.java:88)

Move this call in the onActivityCreated method.

Sign up to request clarification or add additional context in comments.

1 Comment

What helped me in this post was the explicit observation that context == null is true. I checked my context object for being null in an if statement and resolved my issue. Thanks!
11

If you are returning to a fragment while being inside a different fragment, and upon returning to the fragment you execute a method which is causing the

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

Wrapped that method in a

if (getActivity() != null) {
// Code goes here.
}

and problem will be solved.

1 Comment

What is the case in which getActivity() returns null?
5

You can use getActivity(), which returns the activity associated with a fragment. So your code will become:

public AlarmTask(Calendar date) {
this.am = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE); //error for this line
this.date = date;
}

1 Comment

You can have an NPE with this code. It depends when you call getActivity(). You can call it when the activity is not yet defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.