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

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

Posted by: admin May 14, 2020 Leave a comment

Questions:
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);
         }
How to&Answers:

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.

Answer:

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.

Answer:

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;
}