0

I have a class for my user called "User". All getters and setters are correct. I initialized "mUser" in the main activity to be used globally by the app (MainActivity.mUser).

The Log.i("..............UID Used", auth.getCurrentUser().getUid()); returns the correct Uid for the user.

What I am struggling with is nothing executes in the try/catch block. Nothing is logged in catalog and the app crashes. It does not go to onComplete, onFail, nor catches an exception. The app just goes to the next section of code which references the data pulled from this block and gives an error due to the values of mUser all being null.

It was working then all of a sudden stopped and I am unsure why. Can anyone shed some light on this?

private void getUserProfileInfo() {
    MainActivity.mUser.clearUser();
    FirebaseAuth auth = FirebaseAuth.getInstance();
    Log.i("..............UID Used", auth.getCurrentUser().getUid());
    FirebaseDatabase firebaseDatabase = FirebaseDatabase
            .getInstance("https://godsvong-default-rtdb.firebaseio.com/");
    DatabaseReference databaseReference = firebaseDatabase.getReference();
    try {
        databaseReference.child("User_Profiles").child(auth.getCurrentUser().getUid())
                .get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DataSnapshot> task) {
                MainActivity.mUser = task.getResult().getValue(User.class);
                Log.i("Get Data", "SUCCESS!!!!!");
                MainActivity.mUser.showUserDataInCatlog();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.i("Get Data", "FAILED!!!!!");
            }
        });
    }
    catch (Exception e){
        Log.i(".............ERROR", e.toString());
    }

    String path = MainActivity.mUser.getmProfilePicUrl();
    //MainActivity.mUser.showUserDataInCatlog();
    //Log.i(".............PATH", MainActivity.mUser.getmProfilePicUrl());
    //Log.i(".............PATH", path);

    StorageReference gsReference = FirebaseStorage.getInstance()
            .getReferenceFromUrl(path);

Firebase Database

LOGCAT:

W/System: Ignoring header X-Firebase-Locale because its value was null.
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
W/System: Ignoring header X-Firebase-Locale because its value was null.
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/..............UID Used: 3jdmmGvAh4Q8KSvG6m9aSFLOXuG2
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.hsquaredtechnologies.godsvong, PID: 9974
    java.lang.IllegalArgumentException: location must not be null or empty
        at com.google.android.gms.common.internal.Preconditions.checkArgument(com.google.android.gms:play-services-basement@@17.1.0:35)
        at com.google.firebase.storage.FirebaseStorage.getReferenceFromUrl(FirebaseStorage.java:282)
        at com.hsquaredtechnologies.godsvong.ui.logout.LogoutFragment.getUserProfileInfo(LogoutFragment.java:268)
        at com.hsquaredtechnologies.godsvong.ui.logout.LogoutFragment.lambda$onViewCreated$1$LogoutFragment(LogoutFragment.java:186)
        at com.hsquaredtechnologies.godsvong.ui.logout.LogoutFragment$$ExternalSyntheticLambda2.onComplete(Unknown Source:8)
        at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8512)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1139)
I/Process: Sending signal. PID: 9974 SIG: 9
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • "The app just goes to next section of code" That actually is normal, as data is loaded from Firebase asynchronously. But if you set breakpoints in both `onComplete` and `onFailure` and run in the debugger, one of them should be called. Is that not happening for you? – Frank van Puffelen Jul 30 '21 at 23:35
  • Correct. Neither message "SUCCESS!!!!" or "FAILED!!!!" show in catlog. I added the catlog from immediately after I click login in my app. – Victor Humphrey Jul 30 '21 at 23:38
  • You can see where the the UID is logged, then it blows up. Could this be a latency issue causing me to try and retrieve the data before it is placed in the User object? If so, can you guide me on a fix please? – Victor Humphrey Jul 30 '21 at 23:41
  • That exception comes from `getReferenceFromUrl`, which isn't present in the code you shared. So it's hard to say where it goes wrong. (btw: the log output from an Android app is called logcat, not catlog ) – Frank van Puffelen Jul 31 '21 at 01:38
  • LOL!!! My eyes are crossed trying to figure this out. I added the code for the getReferenceFromUrl, but I already know none of the data from Firebase is being added to the mUser object. All parts are null which is where the problem lies. I dont understand where the issue is. – Victor Humphrey Jul 31 '21 at 01:40
  • I replaced the code I originally posted with more. And fixed Logcat. ;) – Victor Humphrey Jul 31 '21 at 01:44
  • 2
    I'm pretty sure the problem is that you access `MainActivity.mUser.getmProfilePicUrl()` before the `MainActivity.mUser = task.getResult().getValue(User.class)` line has executed. Any code that needs data from the database needs to be *inside* the `onComplete` method, or be called from there. See https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Jul 31 '21 at 01:56

0 Answers0