-1

This is my code

public class gsign extends Activity implements ConnectionCallbacks,OnConnectionFailedListener {
private static final int RC_SIGN_IN = 0;
private static final String TAG = "MainActivity";
private GoogleApiClient mGoogleApiClient;
private boolean mIntentInProgress;
private boolean mSignInClicked;
private ConnectionResult mConnectionResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Plus.API)
            .addConnectionCallbacks(this).addOnConnectionFailedListener(this)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

   }

protected void onStart() {
    super.onStart();
    signInWithGplus();
    mGoogleApiClient.connect();
}

protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}
public void onConnectionFailed(ConnectionResult result) {
    if (!result.hasResolution()) {
        GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
                0).show();
        return;
    }
    if (!mIntentInProgress) {
        // Store the ConnectionResult for later usage
        mConnectionResult = result;
        if (mSignInClicked) {
            resolveSignInError();
        }
    }
}
@Override
protected void onActivityResult(int requestCode, int responseCode,
                                Intent intent) {
    if (requestCode == RC_SIGN_IN) {
        if (responseCode != RESULT_OK) {
            mSignInClicked = false;
        }

        mIntentInProgress = false;

        if (!mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.connect();
        }
    }
}

@Override
public void onConnected(Bundle arg0) {
    mSignInClicked = false;
    // Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
    getProfileInformation();
}

@Override
public void onConnectionSuspended(int arg0) {
    mGoogleApiClient.connect();

}
private void signInWithGplus() {
    if (!mGoogleApiClient.isConnecting()) {
        mSignInClicked = true;
        resolveSignInError();
    }
    else
        getProfileInformation();
}

/**
 * Method to resolve any signin errors
 * */
private void resolveSignInError() {
    if (mConnectionResult.hasResolution()) {
        try {
            mIntentInProgress = true;
            mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
        } catch (IntentSender.SendIntentException e) {
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    }
  }
}

I also have code to register the user in my cloud backend and to fetch their information but I am getting the following error

Caused by: java.lang.NullPointerException
        at    com.example.nirmal.loginpage.gsign.resolveSignInError(gsign.java:127)
        at com.example.nirmal.loginpage.gsign.signInWithGplus(gsign.java:117)
        at com.example.nirmal.loginpage.gsign.onCreate(gsign.java:56)

The following error points to if (mConnectionResult.hasResolution())

Normally this error may be caused while calling signInWithGPlus() from the onCreate() method but even after calling it from onStart() I got the same error. The problem is I used the same code in another app(in fact I copied from that one) in which it ran perfectly so I have no idea what caused this error.

I also created separate OAuth client key for this app. So can anyone point me to where I am going wrong????

Nirmal Raj
  • 729
  • 6
  • 18
  • 2
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Jan 29 '16 at 15:30

1 Answers1

2

The stack trace tells it all...

You don't call mGoogleApiClient.connect() until onStart(). Meanwhile back in onCreate(), which is called before onStart(), you call the signInWithGplus(); method.

!mGoogleApiClient.isConnecting() in that method is true leading to resolveSignInError() being called.

You have not yet assigned a value to mConnectionResult so calling mConnectionResult.hasResolution() in resolveSignInError() will throw a NullPointerException.

George Mulligan
  • 11,813
  • 6
  • 37
  • 50
  • I also tried calling signInWithGPlus from onStart().Then also I got the same error – Nirmal Raj Jan 29 '16 at 16:25
  • It has to go after the call to `mGoogleApiClient.connect()` so that `mGoogleApiClient.isConnecting()` will be true by the time you get to your method. – George Mulligan Jan 29 '16 at 16:26
  • Looking at your code are you sure you need to call `signInWithGplus();` at all? Without it it looks like after you call `mGoogleApiClient.connect()` once the `onConnected(...)` callback is reached `getProfileInformation();` is called anyways so I would try removing `signInWithGplus()` first. – George Mulligan Jan 29 '16 at 16:30
  • In the previous app I called the `signInWithGplus()` from a onClicklistener but since here I am making an explicit call I think it will be good to remove the function – Nirmal Raj Jan 29 '16 at 16:40