0

Following google sign in guide I have implemented it in my app and it works as expected however on the first trials I got a null exception when trying to get the profile. So after some search i found GoogleSignInAccount getPhotoUrl() return null and it really appears that there is like a sync issue after removing and adding google account i was managed to properly get the photo url.

Currently the data I am trying to get is in try catch block as below:

            try{
            GoogleSignInAccount acct = result.getSignInAccount();

            //Log.d(TAG,acct.getDisplayName());
            String UserName = acct.getDisplayName();
            String UserEmail=acct.getEmail();
            String UserID=acct.getId();
            String UserPhoto = acct.getPhotoUrl().toString();;

            Log.d(TAG,UserPhoto);
            Log.d(TAG,UserName);
            Log.d(TAG,UserEmail);
            Log.d(TAG,UserID);
        }catch (Exception e){
            e.printStackTrace();

        }

My question is how can i handle a case where part of the information is missing? how can i distinguish between acct.getPhotoUrl().toString(); that can produce NPE and acct.getDisplayName(); that can produce NPE as well?

Anyone has an Idea?.

Community
  • 1
  • 1
user2145673
  • 363
  • 1
  • 9
  • 23
  • Simply add a null check before calling a method on a object that might be null as noted in the documentation: https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInAccount#getPhotoUrl%28%29 – Steven Feb 15 '16 at 06:56
  • So in your code, use something like this instead: String UserPhoto = acct.getPhotoUrl() != null ? acct.getPhotoUrl().toString() : "";, where "" is whatever default you want to use if user has no photo. The same applies for any other fields that might be null – Steven Feb 15 '16 at 07:09
  • Well i ended up doing it with null check as you suggested, you can place it as an answer and I will accept it – user2145673 Feb 24 '16 at 13:05

1 Answers1

0

There has been a thread opened about that.This seems like a bug from Google. I also ended-up with checking every single variable aside but with dummy alternative values. The problem has gone away since i did this and the dummy values have never been used but this is still not convincing.

acct = result.getSignInAccount();
            String id = null;
            try {
                id = acct.getId();
            } catch (Exception e) {
                id = "socialId";
            }
            String email = null;
            try {
                email = acct.getEmail();
            } catch (Exception e) {
                email = "socialEmail";
            }
            String displayName = null;
            try {
                displayName = acct.getDisplayName();
            } catch (Exception e) {
                displayName = "socialFam";
            }
            String familyName = null;
            try {
                familyName = acct.getFamilyName();
            } catch (Exception e) {
                familyName = "socialFam";
            }
            String givenName = null;
            try {
                givenName = acct.getGivenName();
            } catch (Exception e) {
                givenName = "socialGiven";
            }
            String photoUrl = null;
            try {
                photoUrl = acct.getPhotoUrl().toString();
            } catch (Exception e) {
                photoUrl = "socialPhoto";
            }
FRK
  • 76
  • 6