Reputation: 6132
I am trying to save google email, name and profile photo url after a user signs in with Google account. When a user with google's default profile picture signs in, the app crashes. It works with google accounts having a custom profile photo
Here's the relevant code:
private void handleSignInResult(GoogleSignInResult result){
Log.d(TAG,"handleSignInResult():"+result.isSuccess());
if(result.isSuccess()){
GoogleSignInAccount account = result.getSignInAccount();
String pname = account.getDisplayName();
String emailid = account.getEmail();
String pic_url = new String(account.getPhotoUrl().toString());
SharedPreferences userInfo = context.getSharedPreferences(getString(R.string.USER_INFO),Context.MODE_PRIVATE);
SharedPreferences.Editor editor = userInfo.edit();
editor.putString("name",pname);
editor.putString("email",emailid);
editor.putString("pic_url",pic_url);
editor.commit();
updateUI(true);
} else {
updateUI(false);
}
}
Here's is the part of logcat with exception:
--------- beginning of crash
10-16 20:02:33.731 32355 32355 E AndroidRuntime: FATAL EXCEPTION: main
10-16 20:02:33.731 32355 32355 E AndroidRuntime: Process: org.csikjsce.csi_kjsceofficial, PID: 32355
10-16 20:02:33.731 32355 32355 E AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=7, result=-1, data=Intent { (has extras) }} to activity {org.csikjsce.csi_kjsceofficial/org.csikjsce.csi_kjsceofficial.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
10-16 20:02:33.731 32355 32355 E AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:4112)
10-16 20:02:33.731 32355 32355 E AndroidRuntime: at android.app.ActivityThread.handleSendResult(ActivityThread.java:4155)
10-16 20:02:33.731 32355 32355 E AndroidRuntime: at android.app.ActivityThread.access$1600(ActivityThread.java:186)
10-16 20:02:33.731 32355 32355 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1658)
10-16 20:02:33.731 32355 32355 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:111)
10-16 20:02:33.731 32355 32355 E AndroidRuntime: at android.os.Looper.loop(Looper.java:238)
10-16 20:02:33.731 32355 32355 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6016)
10-16 20:02:33.731 32355 32355 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-16 20:02:33.731 32355 32355 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937)
10-16 20:02:33.731 32355 32355 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:798)
10-16 20:02:33.731 32355 32355 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
10-16 20:02:33.731 32355 32355 E AndroidRuntime: at org.csikjsce.csi_kjsceofficial.LoginActivity.handleSignInResult(LoginActivity.java:179)
10-16 20:02:33.731 32355 32355 E AndroidRuntime: at org.csikjsce.csi_kjsceofficial.LoginActivity.onActivityResult(LoginActivity.java:207)
10-16 20:02:33.731 32355 32355 E AndroidRuntime: at android.app.Activity.dispatchActivityResult(Activity.java:6657)
10-16 20:02:33.731 32355 32355 E AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:4108)
10-16 20:02:33.731 32355 32355 E AndroidRuntime: ... 9 more
Line no. 207 calls handleSignResult()
Line no. 179 is String pic_url = new String(account.getPhotoUrl().toString());
Commenting out line 179 fixes the issue but then I cannot save photo url that I need to show user's profile photo in my app.
The error occurs only with users that have the default profile photo on their google account.
I deleted my google plus photo to test it. The app crashed when I signed. Then I checked the photo url on my google plus account from chrome browser and I got this url https://lh3.googleusercontent.com/-eKoJemxhXNk/AAAAAAAAAAI/AAAAAAAAAAA/ACnBePZ9iFrmxpc1l5CPyKJ4DRH14A_Scg/s60-p-rw-no-mo/photo.jpg
The url is similar to the one that gets saved in shared_pref.xml when signing in with an account that has a custom profile photo. The above url is what the getPhotoUrl() must return. Its definitely not null, but still I get NPE on getPhotoUrl().toString() and app crashes.
Can someone explain why this is happening?
Upvotes: 0
Views: 41
Reputation: 6132
Error is solved with this simple trick:
String pic_url = ""+account.getPhotoUrl();
After hunting down for getPhotoUrl() method I found this
I had configured the sign in with DEFAULT_SIGN_IN
flag but the first condition i.e,
if user has a profile picture
is equivalent to user has default profile picture. So all users with default profile picture do not meet the first condition, thus method returns null.
Upvotes: 0