I have integrated google sign-in into my app. I click on the button and choose 'Sign in from different account' it let's the user sign in like usual google sign in and then it crashes.
I have followed the official documentation:
https://developers.google.com/identity/sign-in/android/sign-in
private void googleLogin() {
Intent intent = googleSignInClient.getSignInIntent();
startActivityForResult(intent, GOOGLE_KEY_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GOOGLE_KEY_CODE) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
assert account != null;
String google_email = account.getEmail();
String google_name = account.getDisplayName();
String[] fullname = Objects.requireNonNull(google_name).split(" ");
String firstname = fullname[0];
String lastname = fullname[1];
if (google_email != null) {
loginFromGmail(google_email, firstname, lastname);
Log.d("google_email", google_email);
Log.d("google_email", google_name);
}
} catch (ApiException e) {
e.printStackTrace();
}
}
}
and the error log is -->
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.wars/com.wars.activities.RegisterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] java.lang.String.split(java.lang.String)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4382)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4426)
at android.app.ActivityThread.-wrap20(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1685)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6626)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] java.lang.String.split(java.lang.String)' on a null object reference
at com.wars.activities.RegisterActivity.onActivityResult(RegisterActivity.java:403)
at android.app.Activity.dispatchActivityResult(Activity.java:7305)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4378)
This line causes the error
String[] fullname = Objects.requireNonNull(google_name).split(" ");
If you want to get full name and then split it for first name and last name do this
String first_name ="",last_name="";
String fullname = account.getDisplayName();
try {
if (fullname != null) {
if (!fullname.equalsIgnoreCase("")) {
String[] name_array = fullname.split(" ");
if (name_array.length > 0) {
first_name = name_array[0];
last_name = name_array[1];
}
}
} else {
// do stuff
}
} catch (Exception e) {
e.printStackTrace();
}
Complete code snippet
String firstname="",lastname="";
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GOOGLE_KEY_CODE) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
assert account != null;
String google_email = account.getEmail();
String google_name = account.getDisplayName();
try {
if (google_name != null) {
if (!google_name.equalsIgnoreCase("")) {
String[] name_array = google_name.split(" ");
if (name_array.length > 0) {
firstname= name_array[0];
lastname = name_array[1];
}
}
} else {
// handle the null case in case user does not have display name in gmail account
google_name = "";
firstname= "";
lastname = "";
}
} catch (Exception e) {
e.printStackTrace();
}
if (google_email != null) {
loginFromGmail(google_email, firstname, lastname);
Log.d("google_email", google_email);
Log.d("google_email", google_name);
}
} catch (ApiException e) {
e.printStackTrace();
}
}
}