相关文章推荐
帅气的葡萄  ·  html - Uncaught ...·  9 月前    · 
文质彬彬的斑马  ·  readdirectorychangesw ...·  1 年前    · 
英勇无比的铁链  ·  使用 white-space ...·  1 年前    · 
爽快的野马  ·  java中Number类理解 - ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

So I'm trying to implement gcm or my app with the help of this documentation . However, when trying to launch my application, I get following error:

 java.lang.RuntimeException: An error occured while executing doInBackground()
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference

Here is the code in question:

private static int getAppVersion(Context context) {
        try {
            PackageInfo packageInfo = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            // should never happen
            throw new RuntimeException("Could not get package name: " + e);

What is my mistake here?

EDIT: Where I'm using "getAppVersion":

private String getRegistrationId(Context context) {
        final SharedPreferences prefs = getGCMPreferences(context);
        String registrationId = prefs.getString(PROPERTY_REG_ID, "");
        if (registrationId.isEmpty()) {
            Log.i(TAGD, "Registration not found.");
            return "";
        // Check if app was updated; if so, it must clear the registration ID
        // since the existing registration ID is not guaranteed to work with
        // the new app version.
        int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
        int currentVersion = getAppVersion(context);
        if (registeredVersion != currentVersion) {
            Log.i(TAGD, "App version changed.");
            return "";
        return registrationId;
private void storeRegistrationId(Context context, String regId) {
        final SharedPreferences prefs = getGCMPreferences(context);
        int appVersion = getAppVersion(context);
        Log.i(TAGD, "Saving regId on app version " + appVersion);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(PROPERTY_REG_ID, regId);
        editor.putInt(PROPERTY_APP_VERSION, appVersion);
        editor.apply();

Where I start getRegistrationId: in the onCreate method,

if (checkPlayServices()) {
            gcm = GoogleCloudMessaging.getInstance(this);
            regid = getRegistrationId(context);
            if (regid.isEmpty()) {
                registerInBackground();
        } else {
            Log.v(TAGD, "No valid GPS APK found.");
                That actually didn't help a lot. Where is context coming from? Either it is null, or it is not yet ready for use (e.g., you are calling it from a data member initializer of an activity or service).
– CommonsWare
                May 24, 2015 at 20:23
                ... and now show where you are calling storeRegistrationId. A null Context is getting passed at some point - I notice the docs are kind of incomplete - in their example they are passing a context but never show how it was obtained.
– trooper
                May 24, 2015 at 20:23
                "Context is just defined in the class as Context context" -- and where are you assigning a value to it?
– CommonsWare
                May 24, 2015 at 21:10
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.