Null pointer exception in JobIntentService onDestroy

432 views Asked by At

I am facing issue with JobIntentService in Android 8.1 OS with Signed apk. Where as its working fine with the debug version. Tried with multiple things but couldnt get solution for this. Here is the crash log i got from Crashalytics.

java.lang.RuntimeException: Unable to stop service com.app.xmpp.XmppService@241c51c6: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.JobIntentService$WorkEnqueuer.serviceProcessingFinished()' on a null object reference

        at android.app.ActivityThread.handleStopService(ActivityThread.java:3284)

        at android.app.ActivityThread.access$2300(ActivityThread.java:188)

        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1633)

        at android.os.Handler.dispatchMessage(Handler.java:111)

        at android.os.Looper.loop(Looper.java:210)

        at android.app.ActivityThread.main(ActivityThread.java:5839)

        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:1113)

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.JobIntentService$WorkEnqueuer.serviceProcessingFinished()' on a null object reference
        at android.support.v4.app.JobIntentService.onDestroy(JobIntentService.java:479)
        at com.app.xmpp.XmppService.onDestroy(XmppService.java:91)
        at android.app.ActivityThread.handleStopService(ActivityThread.java:3265)
        at android.app.ActivityThread.access$2300(ActivityThread.java:188) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1633) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:210) 
        at android.app.ActivityThread.main(ActivityThread.java:5839) 
        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:1113)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879) 

Help me if anyone faced similar issues with JobIntentService.

Please note this problem cannot be resolved with handling Nullpointer exception, because the NPE is occurring at JobIntentService which is part of Android Framework and we cannot modify it and this is particularly happening only in Signed APK.

XMPP Service OnDestroy

@Override
    public void onDestroy() {
        super.onDestroy();

        MyXMPP.instance=null;
        MyXMPP.instanceCreated=false;

        MyXMPP.connection.disconnect();
        xmppConnectionCheckHandler.removeCallbacks(xmppConnectionCheckRunnable);

        System.out.println("--------------Xmpp Service Stopped-----------");
    }

Thanks in advance.

1

There are 1 answers

4
Andrey Busik On

From JobIntentService class:

@Override
public void onDestroy() {
    super.onDestroy();
    if (mCompatQueue != null) {
        synchronized (mCompatQueue) {
            mDestroyed = true;
            mCompatWorkEnqueuer.serviceProcessingFinished(); //line 479
        }
    }
}

@Override
public void onCreate() {
    super.onCreate();
    if (DEBUG) Log.d(TAG, "CREATING: " + this);
    if (Build.VERSION.SDK_INT >= 26) {
        mJobImpl = new JobServiceEngineImpl(this);
        mCompatWorkEnqueuer = null;
    } else {
        mJobImpl = null;
        ComponentName cn = new ComponentName(this, this.getClass());
        mCompatWorkEnqueuer = getWorkEnqueuer(this, cn, false, 0);
    }
}

Do you perform any manipulation with mCompatWorkEnqueuer or mCompatQueue in your subclass? Say, like setting it to null or super.onCreate() invocation?


Pay attention that execution of line 479 depends on existence of value of mCompatQueue. Also mCompatWorkEnqueuer is set to null in onCreate for API's 26 (8.0) and above