title: Sign Out From Google plus Account Not Working in android, Showing Null Pointer Exception
body: <p>I am using Google plus login in my android app its Working Fine.</p>
  
  <p>While Clicking logout Button from any other intent Showing Null pointer Exception.</p>
  
  <p>also While click login button directly going to my next page .not in sign in page.</p>
  
  <p>but Logout from Demo Application Its working fine.</p>
  
  <p>I Have searched  related  answers ,Not found any solution still remain same.</p>
  
  <p>help me to resolve it</p>
  
  <p><a href="http://examples.javacodegeeks.com/android/android-google-plus-login-example/" rel="nofollow">link 1</a> </p>
  
  <p><a href="http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/" rel="nofollow">link 2</a></p>
  
  <p>I have referred 2 links but sign out from next intent still showing the Null pointer Exception.</p>
  
  <p>My Logout Code:</p>
  
  <pre><code>public void googlePlusLogout() 
      {
          if (mGoogleApiClient.isConnected()) 
          {
              Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
              mGoogleApiClient.disconnect();
              mGoogleApiClient.connect();
              updateProfile(false);
          }
  
      }
  </code></pre>
  
  <p>here calling the method from other intent to signin class</p>
  
  <p>otherintent.java</p>
  
  <pre><code>                  session.logoutUser();// here i am clearing the shared pref
                    sign_in Signinclass = new sign_in();
                    Signinclass.googlePlusLogout();
                    finish(); 
  </code></pre>
  
  <p>This is my demo code its working fine Same Code I Have used in my Application Login working fine ,but Logout calling from another page not working. Showing null pointer exception. </p>
  
  <p>AndroidGooglePlusExample .java</p>
  
  <pre><code>public class AndroidGooglePlusExample extends Activity implements OnClickListener, ConnectionCallbacks, OnConnectionFailedListener {
  
      private static final int RC_SIGN_IN = 0;
  
      // Google client to communicate with Google
      private GoogleApiClient mGoogleApiClient;
  
      private boolean mIntentInProgress;
      private boolean signedInUser;
      private ConnectionResult mConnectionResult;
      private SignInButton signinButton;
      private ImageView image;
      private TextView username, emailLabel;
      private LinearLayout profileFrame, signinFrame;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          signinButton = (SignInButton) findViewById(R.id.signin);
          signinButton.setOnClickListener(this);
  
          image = (ImageView) findViewById(R.id.image);
          username = (TextView) findViewById(R.id.username);
          emailLabel = (TextView) findViewById(R.id.email);
  
          profileFrame = (LinearLayout) findViewById(R.id.profileFrame);
          signinFrame = (LinearLayout) findViewById(R.id.signinFrame);
  
          mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API, Plus.PlusOptions.builder().build()).addScope(Plus.SCOPE_PLUS_LOGIN).build();
      }
  
      protected void onStart() {
          super.onStart();
          mGoogleApiClient.connect();
      }
  
      protected void onStop() {
          super.onStop();
          if (mGoogleApiClient.isConnected()) {
              mGoogleApiClient.disconnect();
          }
      }
  
      private void resolveSignInError() {
          if (mConnectionResult.hasResolution()) {
              try {
                  mIntentInProgress = true;
                  mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
              } catch (SendIntentException e) {
                  mIntentInProgress = false;
                  mGoogleApiClient.connect();
              }
          }
      }
  
      @Override
      public void onConnectionFailed(ConnectionResult result) {
          if (!result.hasResolution()) {
              GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
              return;
          }
  
          if (!mIntentInProgress) {
              // store mConnectionResult
              mConnectionResult = result;
  
              if (signedInUser) {
                  resolveSignInError();
              }
          }
      }
  
      @Override
      protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
          switch (requestCode) {
          case RC_SIGN_IN:
              if (responseCode == RESULT_OK) {
                  signedInUser = false;
  
              }
              mIntentInProgress = false;
              if (!mGoogleApiClient.isConnecting()) {
                  mGoogleApiClient.connect();
              }
              break;
          }
      }
  
      @Override
      public void onConnected(Bundle arg0) {
          signedInUser = false;
          Toast.makeText(this, "Connected", Toast.LENGTH_LONG).show();
          getProfileInformation();
      }
  
      private void updateProfile(boolean isSignedIn) {
          if (isSignedIn) {
              signinFrame.setVisibility(View.GONE);
              profileFrame.setVisibility(View.VISIBLE);
  
          } else {
              signinFrame.setVisibility(View.VISIBLE);
              profileFrame.setVisibility(View.GONE);
          }
      }
  
      private void getProfileInformation() {
          try {
              if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                  Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
                  String personName = currentPerson.getDisplayName();
                  String personPhotoUrl = currentPerson.getImage().getUrl();
                  String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
  
                  username.setText(personName);
                  emailLabel.setText(email);
  
                  new LoadProfileImage(image).execute(personPhotoUrl);
  
                  // update profile frame with new info about Google Account
                  // profile
                  updateProfile(true);
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      @Override
      public void onConnectionSuspended(int cause) {
          mGoogleApiClient.connect();
          updateProfile(false);
      }
  
      @Override
      public void onClick(View v) {
          switch (v.getId()) {
          case R.id.signin:
              googlePlusLogin();
              break;
          }
      }
  
      public void signIn(View v) {
          googlePlusLogin();
      }
  
      public void logout(View v) {
          googlePlusLogout();
      }
  
      private void googlePlusLogin() {
          if (!mGoogleApiClient.isConnecting()) {
              signedInUser = true;
              resolveSignInError();
          }
      }
  
      private void googlePlusLogout() {
          if (mGoogleApiClient.isConnected()) {
              Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
              mGoogleApiClient.disconnect();
              mGoogleApiClient.connect();
              updateProfile(false);
          }
      }
  
      // download Google Account profile image, to complete profile
      private class LoadProfileImage extends AsyncTask&lt;String, Void, Bitmap&gt; {
          ImageView downloadedImage;
  
          public LoadProfileImage(ImageView image) {
              this.downloadedImage = image;
          }
  
          protected Bitmap doInBackground(String... urls) {
              String url = urls[0];
              Bitmap icon = null;
              try {
                  InputStream in = new java.net.URL(url).openStream();
                  icon = BitmapFactory.decodeStream(in);
              } catch (Exception e) {
                  Log.e("Error", e.getMessage());
                  e.printStackTrace();
              }
              return icon;
          }
  
          protected void onPostExecute(Bitmap result) {
              downloadedImage.setImageBitmap(result);
          }
      }
  </code></pre>