-1

I'm building an app with a JobService. This JobService should to start a service every 1 minutes.

This is my JobService class:

public class BleJobService extends JobService {
    JobParameters params;
    DoItTask doIt;
    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        this.params = params;
        Log.d("TestService", "Work to be called from here");
        startService(new Intent(this, BlePowerService_JobScheduler.class));

        /*doIt = new DoItTask();
        doIt.execute();*/
        jobFinished(params, false);
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        Log.d("TestService", "System calling to stop the job here");
        if (doIt != null)
            doIt.cancel(true);
        return false;
    }

    private class DoItTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPostExecute(Void aVoid) {
            Log.d("DoItTask", "Clean up the task here and call jobFinished...");

            jobFinished(params, false);
            super.onPostExecute(aVoid);
        }

        @Override
        protected Void doInBackground(Void... params) {
            Log.d("DoItTask", "Working here...");
            return null;
        }
    }
}

But if I try to start this JobService I have this error:

FATAL EXCEPTION: main
                  Process: com.eresult.diabesitycare.devicesensor, PID: 15938
                  java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.job.IJobCallback android.app.job.JobParameters.getCallback()' on a null object reference
                      at android.app.job.JobService$JobHandler.handleMessage(JobService.java:161)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6776)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)

The problem is on this line code:

 jobFinished(params, false);

params is null

bircastri
  • 2,169
  • 13
  • 50
  • 119
  • 1
    may be? JobParameters params = new JobParameters(); and `this.params = params`? should not it be `this.params = jobParameters`? – Masoom Badi May 15 '18 at 09:09

1 Answers1

1

this.params = params (unless params coming from somewhere else) is a self-assignment.

Given the fact the it isn't initially instantiated the reference remains null, and thus the NPE.

You probably mean

this.params = jobParameters;
payloc91
  • 3,724
  • 1
  • 17
  • 45