Skip to content
This repository has been archived by the owner on Sep 14, 2021. It is now read-only.

user.getPhotoUrl().toString() null pointer exception #27

Closed
gkaldev opened this issue Dec 18, 2017 · 6 comments
Closed

user.getPhotoUrl().toString() null pointer exception #27

gkaldev opened this issue Dec 18, 2017 · 6 comments

Comments

@gkaldev
Copy link
Contributor

gkaldev commented Dec 18, 2017

From @VihaanVerma89 on May 29, 2016 17:6

I'm getting null pointer exception on loading image post. Uri returned from user.getPhotoUrl() is null

    public static Author getAuthor() {
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if (user == null) return null;
        return new Author(user.getDisplayName(), user.getPhotoUrl().toString(), user.getUid());
    }

screenshot from 2016-05-29 22 32 47

Logcat

05-29 13:05:51.309 15438-15955/com.google.firebase.samples.apps.friendlypix E/AndroidRuntime: FATAL EXCEPTION: FirebaseStorage-Callbacks-1
                                                                                              Process: com.google.firebase.samples.apps.friendlypix, PID: 15438
                                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
                                                                                                  at com.google.firebase.samples.apps.friendlypix.FirebaseUtil.getAuthor(FirebaseUtil.java:45)
                                                                                                  at com.google.firebase.samples.apps.friendlypix.NewPostUploadTaskFragment$UploadPostTask$2$2.onSuccess(NewPostUploadTaskFragment.java:182)
                                                                                                  at com.google.firebase.samples.apps.friendlypix.NewPostUploadTaskFragment$UploadPostTask$2$2.onSuccess(NewPostUploadTaskFragment.java:174)
                                                                                                  at com.google.firebase.storage.StorageTask$1.zza(Unknown Source)
                                                                                                  at com.google.firebase.storage.StorageTask$1.zzl(Unknown Source)
                                                                                                  at com.google.firebase.storage.zze$2.run(Unknown Source)
                                                                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                                  at java.lang.Thread.run(Thread.java:818)

Copied from original issue: googlearchive/friendlypix#12

@gkaldev
Copy link
Contributor Author

gkaldev commented Dec 18, 2017

From @tikurahul on May 29, 2016 18:16

This is a bug in the sample code. FirebaseUser#getPhotoUrl actually returns a nullable Uri.
Replace user.getPhotoUrl().toString() with user.getPhotoUrl() != null ? user.getPhotoUrl().toString() : null.

Better yet, you can submit a PR to fix the issue. 👍

@gkaldev
Copy link
Contributor Author

gkaldev commented Dec 18, 2017

From @gkfillipus on August 5, 2016 21:48

Hi i am also having the same problem with this code similar to above post

FirebaseUser mUser = mFirebaseAuth.getCurrentUser();
if (mUser != null) {
// User is signed in
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
String uid = mFirebaseAuth.getCurrentUser().getUid();
THIS LINE String image = mFirebaseAuth.getCurrentUser().getPhotoUrl().toString();
intent.putExtra("user_id", uid);
if(image!=null){
intent.putExtra("profile_picture",image);
}
startActivity(intent);
startActivity(intent);
finish();
Log.d(TAG, "onAuthStateChanged:signed_in:" + mUser.getUid());
}

any help

@gkaldev
Copy link
Contributor Author

gkaldev commented Dec 18, 2017

From @jamespinto on January 20, 2017 13:11

What is the solution for this?

the following suggestion by tikurahul dint work

Replace user.getPhotoUrl().toString() with user.getPhotoUrl() != null ? user.getPhotoUrl().toString() : null

@gkaldev
Copy link
Contributor Author

gkaldev commented Dec 18, 2017

From @migueltarga on January 20, 2017 13:19

public static Author getAuthor() {
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if (user == null) return null;
        return new Author(user.getDisplayName(), user.getPhotoUrl() != null ? user.getPhotoUrl().toString() : null, user.getUid());
    }

@jamespinto I agree with @tikurahul this should fix it, if the error in this line is:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference

@gkaldev
Copy link
Contributor Author

gkaldev commented Dec 18, 2017

From @jamespinto on January 20, 2017 13:34

nope! not working. I am using the google signin provider to log the user in.
According to the google documentation (the below code also gives me the same nullpointer exception)

Get a user's provider-specific profile information

To get the profile information retrieved from the sign-in providers linked to a user, use the getProviderData method. For example:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    for (UserInfo profile : user.getProviderData()) {
        // Id of the provider (ex: google.com)
        String providerId = profile.getProviderId();

        // UID specific to the provider
        String uid = profile.getUid();

        // Name, email address, and profile photo Url
        String name = profile.getDisplayName();
        String email = profile.getEmail();
        Uri photoUrl = profile.getPhotoUrl();
    };
}

https://firebase.google.com/docs/auth/android/manage-users#get_a_users_provider-specific_profile_information

@gkaldev
Copy link
Contributor Author

gkaldev commented Feb 21, 2018

Cleaning up issue fixed in upcoming v2.

@gkaldev gkaldev closed this as completed Feb 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant