0

I am currently having trouble with a NullPointerException when I am attempting to call onConnectionFailed.

I've followed the guidelines for this particular GoogleApi function, and updated its depreciated call.

However, when I try to call it, it gives me a NullPointerException, even though I think I covered all the pointers and initialised them properly.

This is my onConnectionFailed implementation:

public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    if (connectionResult.hasResolution()) {
        try {
            connectionResult.startResolutionForResult(this, RESOLVE_CONNECTION_REQUEST_CODE);
        } catch (IntentSender.SendIntentException e) {
            FailedConnection();
        }
    } else {
        GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
        int StatusCode = apiAvailability.isGooglePlayServicesAvailable(this);
        apiAvailability.getErrorDialog(this, StatusCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();
    }
}

I initially thought this was a problem with global variables and local variables, though I've copied the pointer call down to the local scope and it still gives me a NullPointerException.

This is the logcat, if anyone needs it:

y.phonebelt E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=0, data=null} to activity {com.example.denny.phonebelt/com.example.denny.phonebelt.ViewingWindow}: java.lang.NullPointerException
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3500)
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3543)
     at android.app.ActivityThread.access$1200(ActivityThread.java:159)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:137)
     at android.app.ActivityThread.main(ActivityThread.java:5419)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:525)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
     at com.example.denny.phonebelt.ViewingWindow.onConnectionFailed(ViewingWindow.java:214)
     at com.google.android.gms.internal.zzpp.zza(Unknown Source)
     at com.google.android.gms.internal.zzps.onActivityResult(Unknown Source)
     at com.google.android.gms.internal.zzra.onActivityResult(Unknown Source)
     at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:165)
     at android.app.Activity.dispatchActivityResult(Activity.java:5563)
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3496)
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3543) 
     at android.app.ActivityThread.access$1200(ActivityThread.java:159) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:5419) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:525) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
     at dalvik.system.NativeStart.main(Native Method) 
Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
Mildwood
  • 349
  • 2
  • 13

1 Answers1

0

Could you please try the code below?

According to DOCS, method getErrorDialog() can return null if errorCode == SUCCESS

public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    if (connectionResult.hasResolution()) {
        try {
            connectionResult.startResolutionForResult(this, RESOLVE_CONNECTION_REQUEST_CODE);
        } catch (IntentSender.SendIntentException e) {
            FailedConnection();
        }
    } else {

        GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
        int StatusCode = apiAvailability.isGooglePlayServicesAvailable(this);

        if(StatusCode != ConnectionResult.SUCCESS)
            apiAvailability.getErrorDialog(this, StatusCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();
    }
}

Another option is connectionResult == null but it is not possible to check how you defined it..

guipivoto
  • 18,327
  • 9
  • 60
  • 75