Answers for "Attempt to invoke virtual method 'java.lang.String com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken()' on a null object reference"

0

Attempt to invoke virtual method 'java.lang.String com.google.android.gms.auth.api.signin.GoogleSignInAccount.getIdToken()' on a null object reference com.pravin.yashlalit.msbtestudymaterial.Authentications.SigninActivity.FirebaseGoogleAuth

public class SigninActivity extends AppCompatActivity {

    SignInButton signInButton;
    GoogleSignInClient mGoogleSignInClient;
    String TAG="SigninActivity";
    FirebaseAuth mAuth;
    int RC_SIGN_IN =1;
    String personName,personGivenName,personEmail,personId;
    Uri personPhoto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signin);

        initializations();

    }

    private void initializations() {
        signInButton = findViewById(R.id.google_btn);
        mAuth = FirebaseAuth.getInstance();

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(this,gso);

        signInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signIn();
            }
        });
    }

    private void signIn() {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent,RC_SIGN_IN);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode==RC_SIGN_IN)
        {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleSignInResult(task);
        }
    }

    private void handleSignInResult(Task<GoogleSignInAccount> task) {
        try{
            GoogleSignInAccount acc = task.getResult(ApiException.class);
            Toast.makeText(getApplicationContext(),"Signing Success",Toast.LENGTH_SHORT).show();
            FirebaseGoogleAuth(acc);
        }catch(ApiException e)
        {
            Toast.makeText(getApplicationContext(),"Signing FAiled",Toast.LENGTH_SHORT).show();
            FirebaseGoogleAuth(null);
        }
    }

    private void FirebaseGoogleAuth(GoogleSignInAccount acc) {
        AuthCredential authCredential = GoogleAuthProvider.getCredential(acc.getIdToken(),null);
        mAuth.signInWithCredential(authCredential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                  if (task.isSuccessful())
                  {
                      Toast.makeText(getApplicationContext(),"Firebase Success",Toast.LENGTH_SHORT).show();
                      FirebaseUser user = mAuth.getCurrentUser();
                      updateUI(user);
                  }
                  else
                  {
                      Toast.makeText(getApplicationContext(),"Firebase Failed",Toast.LENGTH_SHORT).show();
                      updateUI(null);
                  }
            }
        });
    }

    private void updateUI(FirebaseUser user) {
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(getApplicationContext());
        if (account != null)
        {
             personName = account.getDisplayName();
             personGivenName = account.getGivenName();
             personEmail = account.getEmail();
             personId = account.getId();
             personPhoto = account.getPhotoUrl();

          /*  Toast.makeText(getApplicationContext(),"User Information:" +
                      "\nName: "+personName+
                    "\nGiven: "+personGivenName+
                    "\nEmail: "+personEmail+
                    "\nId: "+personId+
                    "\nPhoto: "+personPhoto.toString(),
                    Toast.LENGTH_SHORT).show();*/

            sendUserToSetupActivity(personName,personEmail,personPhoto);

        }


    }

    private void sendUserToSetupActivity(String personName, String personEmail, Uri personPhoto) {
        startActivity(new Intent(getApplicationContext(),SetupActivity.class)
                        .putExtra("Name",personName)
                        .putExtra("Email",personEmail)
                        .putExtra("Photo",personPhoto.toString())
                     );
        this.finish();

    }

    @Override
    protected void onStart() {
        super.onStart();

        FirebaseUser currentUser = mAuth.getCurrentUser();

        if (currentUser != null) {
            sendUserToMainActivity();
        }
    }
    private void sendUserToMainActivity() {
        startActivity(new Intent(getApplicationContext(), MainActivity.class));
        this.finish();
    }
}
Posted by: Guest on September-10-2020

Code answers related to "Attempt to invoke virtual method 'java.lang.String com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken()' on a null object reference"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language