GoogleCloudMessaging InstanceID.getInstance returns null

1.9k views Asked by At

I've been looking around for the problem I'm facing but have yet to find anything pertaining to what I'm getting from trying to get the InstanceID from GoogleCloudMessaging. I've tried setting up the call in a class extending IntentService and also tried using AsyncTask to call it in the background, but to no avail.

I've been following tutorials on how to set up the Manifest and the application in order to obtain the right information. Anyone have any clue what could be wrong? My logs say that there is a nullPointerException at InstanceID.getInstance(getApplicationContext());

my call:

private class RegisterGCM extends IntentService{
    public RegisterGCM(){
        super("GcmIntentService");
        registerDevice();
    }
    private void registerDevice(){
        InstanceID instanceId = InstanceID.getInstance(getApplicationContext());

        //Constants contains GCM_SENDER_ID which is the project number from Google Developer Console
        String token = instanceId.getToken(Constants.GCM_SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE);
    }
}

I put this class in my LandingPage class after logging in and call it by:

new RegisterGCM();

This is called right after checking if the play services are up to date and updated.

Any ideas? If I need to provide more information I can, but this is just a snippet of what I can post.

EDIT: Tried adding intent-filter to service in the Manifest and checking if getApplicationContext() is null and this is what the exception coming back tells me:

06-15 16:42:38.614: W/System.err(17295): java.lang.NullPointerException:   Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
06-15 16:42:38.624: W/System.err(17295):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:110)
06-15 16:42:38.624: W/System.err(17295):    at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
06-15 16:42:38.624: W/System.err(17295):    at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
06-15 16:42:38.624: W/System.err(17295):    at .gcmnotification.GcmIntentService.registerDevice(GcmIntentService.java:33)
06-15 16:42:38.624: W/System.err(17295):    at .gcmnotification.GcmIntentService.<init>(GcmIntentService.java:29)
06-15 16:42:38.624: W/System.err(17295):    at .LandingPageActivity.onCreate(LandingPageActivity.java:115)
06-15 16:42:38.624: W/System.err(17295):    at android.app.Activity.performCreate(Activity.java:6289)
06-15 16:42:38.624: W/System.err(17295):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
06-15 16:42:38.624: W/System.err(17295):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
06-15 16:42:38.624: W/System.err(17295):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2777)
06-15 16:42:38.624: W/System.err(17295):    at android.app.ActivityThread.access$900(ActivityThread.java:179)
06-15 16:42:38.624: W/System.err(17295):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462)
06-15 16:42:38.624: W/System.err(17295):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-15 16:42:38.624: W/System.err(17295):    at android.os.Looper.loop(Looper.java:145)
06-15 16:42:38.624: W/System.err(17295):    at android.app.ActivityThread.main(ActivityThread.java:5972)
06-15 16:42:38.624: W/System.err(17295):    at java.lang.reflect.Method.invoke(Native Method)
06-15 16:42:38.624: W/System.err(17295):    at java.lang.reflect.Method.invoke(Method.java:372)
06-15 16:42:38.624: W/System.err(17295):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
06-15 16:42:38.624: W/System.err(17295):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

this is the error message returned from my catch block: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

So I'm guessing the getApplicationContext is returning null?

2

There are 2 answers

2
AllDayAmazing On

According to the guide, make sure your IntentService has the

<intent-filter>
             <action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>

permission. Also, when are you calling the service? If you didn't explicitly launch it as an exported=false in the manifest, getApplicationContext() may be returning null. Stepping through the code it gets obfuscated but it looks like you might be better off using a reference to the Context of the Service as a few steps down the line it calls getApplicationContext() from the Context you pass in and that is most likely where your null object is coming from.

I think this call returns null for the Application class.

Look at the constructor here: Application class and then it's super: ContextWrapper, then the call getApplicationContext() calls the mBase object, which looks like it is null, hence the NPE.

1
Arthur Thompson On

You should not be creating an instance of RegisterGCM(). It should be started with a call to startService() from your "Landing Page":

if (checkPlayServices()) {
    // Start IntentService to register this application with GCM.
    Intent intent = new Intent(this, RegisterGCM.class);
    startService(intent);
}

See a full example here