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?.
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