NullPointerException in onHandleIntent android

HI,
I am getting the following exception :
java.lang.NullPointerException
       at co.riva.vader.PackageUpdateService.onHandleIntent(PackageUpdateService.java:18)
       at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
       at android.os.Handler.dispatchMessage(Handler.java:119)
       at android.os.Looper.loop(Looper.java:137)
       at android.os.HandlerThread.run(HandlerThread.java:60)

Open in new window


package co.riva.vader;

import android.app.IntentService;
import android.content.Intent;

import co.riva.vader.medusa.event.AppInstall;
import co.riva.vader.medusa.event.AppUninstall;

public class PackageUpdateService extends IntentService {
    private static final String SERVICE_NAME = "package-update-service";

    public PackageUpdateService() {
        super(SERVICE_NAME);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        String packageName = intent.getDataString().replace("package:", "");
        VaderApp.getLauncherActivitiesManager().update(getPackageManager());
        VaderApp.getLauncherAppsManager().updatePackage(getApplicationContext().getPackageManager(), packageName);
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
            if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
                VaderApp.getFavouritesManager().removePackage(packageName);
                VaderApp.getIconManager().removePackage(packageName);
                VaderApp.getMedusaService().report(new AppUninstall(packageName, VaderApp.getLauncherAppsManager().getAll().size()));
            }
        } else if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
            if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
                VaderApp.getIconManager().loadAppIcons(packageName);
                VaderApp.getMedusaService().report(new AppInstall(packageName, VaderApp.getLauncherAppsManager().getAll().size()));
            }
        } else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
            VaderApp.getIconManager().loadAppIcons(packageName);
        }
    }
}

Open in new window


I found something on android developers site :

public int onStartCommand (Intent intent, int flags, int startId)
Added in API level 5

You should not override this method for your IntentService. Instead, override onHandleIntent(Intent), which the system calls when the IntentService receives a start request.
Parameters
intent       The Intent supplied to startService(Intent), as given. This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY.
flags       Additional data about this start request. Currently either 0, START_FLAG_REDELIVERY, or START_FLAG_RETRY.
startId       A unique integer representing this specific request to start. Use with stopSelfResult(int).
Returns

    The return value indicates what semantics the system should use for the service's current started state. It may be one of the constants associated with the START_CONTINUATION_MASK bits.

See Also

    onStartCommand(Intent, int, int)


This says something about how one can get null Intent. But i couldnt understand how to avoid this exception.
Thanks
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris Harte2015 Top Expert (Most Article Points)Commented:
You are calling super class "package-update-service". If this does not have a method of onHandleIntent() then you cannot override it. Normally you just override onHandleIntent without calling a super class.

(According to the documentation https://developer.android.com/training/run-background-service/create-service.html)
mccarlIT Business Systems Analyst / Software DeveloperCommented:
This says something about how one can get null Intent. But i couldnt understand how to avoid this exception.

Just wrap the code inside "onHandleIntent" in an if statement to check for intent != null, ie...

package co.riva.vader;

import android.app.IntentService;
import android.content.Intent;

import co.riva.vader.medusa.event.AppInstall;
import co.riva.vader.medusa.event.AppUninstall;

public class PackageUpdateService extends IntentService {
    private static final String SERVICE_NAME = "package-update-service";

    public PackageUpdateService() {
        super(SERVICE_NAME);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            String packageName = intent.getDataString().replace("package:", "");
            VaderApp.getLauncherActivitiesManager().update(getPackageManager());
                VaderApp.getLauncherAppsManager().updatePackage(getApplicationContext().getPackageManager(), packageName);
            if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
                if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
                    VaderApp.getFavouritesManager().removePackage(packageName);
                    VaderApp.getIconManager().removePackage(packageName);
                    VaderApp.getMedusaService().report(new AppUninstall(packageName, VaderApp.getLauncherAppsManager().getAll().size()));
                }
            } else if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
                if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
                    VaderApp.getIconManager().loadAppIcons(packageName);
                    VaderApp.getMedusaService().report(new AppInstall(packageName, VaderApp.getLauncherAppsManager().getAll().size()));
                }
            } else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
                VaderApp.getIconManager().loadAppIcons(packageName);
            }
        }
    }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Android

From novice to tech pro — start learning today.