I am new to java and today I received this error: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.common.SignInButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference I thought I would have a reference with the findviewbyid command but somehow it is not working. Could someone help me?
package com.~;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
public class MainActivity extends AppCompatActivity {
//Google Sign In
private SignInButton googleSignIn;
private GoogleSignInClient googleSignInClient;
private FirebaseAuth firebaseAuth;
private static final String TAG= "Google Sign In Tag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
googleSignIn = findViewById(R.id.googleSignInButton1);
//configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
googleSignInClient = GoogleSignIn.getClient(this, gso);
//int Firebase Auth
firebaseAuth = FirebaseAuth.getInstance();
//Google Sign In Button
googleSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//begin Google Sign In
Log.d(TAG,"onClick: begin Google Sign In");
Intent intent = googleSignInClient.getSignInIntent();
resultLauncher.launch(new Intent(googleSignInClient.getSignInIntent()));
}
});
}
ActivityResultLauncher<Intent> resultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if(result.getResultCode() == Activity.RESULT_OK){
Intent intent = result.getData();
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(intent);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());
firebaseAuthWithGoogle(account.getIdToken());
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
}
}
}
});
private void firebaseAuthWithGoogle(String idToken) {
Log.d(TAG,"firebaseAuthWithGoogleAccount: begin firebase auth with google account");
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
//get logged in user
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
//get user info
String uid = firebaseUser.getUid();
String email = firebaseUser.getEmail();
//check if user is new or existing
//start Profile Activity
startActivity(new Intent(MainActivity.this, Timer.class));
finish();
//updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
//updateUI(null);
}
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context=".MainActivity">
<layout></layout>
<ImageView
android:id="@+id/googleLogo"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/googleLogo"
android:text="Welcome"
style="@style/TextAppearance.AppCompat.Body1"/>
<com.google.android.gms.common.SignInButton
android:id="@+id/googleSignInButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>