Skip to content
This repository has been archived by the owner on Jul 24, 2020. It is now read-only.

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

Closed
VihaanVerma89 opened this issue May 29, 2016 · 6 comments
Closed

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

VihaanVerma89 opened this issue May 29, 2016 · 6 comments
Assignees

Comments

@VihaanVerma89
Copy link

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)
@tikurahul
Copy link

tikurahul commented May 29, 2016

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 gkaldev self-assigned this May 31, 2016
@geraldokandonga
Copy link

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

@jamespinto
Copy link

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

@migueltarga
Copy link

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

@jamespinto
Copy link

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

gkaldev commented Dec 18, 2017

This issue was moved to googlearchive/friendlypix-android#27

@gkaldev gkaldev closed this as completed Dec 18, 2017
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

6 participants