Home:ALL Converter>Android studio firebase auth uid using in database

Android studio firebase auth uid using in database

Ask Time:2020-06-12T17:39:13         Author:Ali Ghassan

Json Formatter

l am try to build app via android studio . l want to display user profile using Firebase auth uid . But i have fatal :

FATAL EXCEPTION: main
    Process: al.ff.boutiqueshopping, PID: 2728
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at al.ff.boutiqueshopping.UserProfile$1.onDataChange(UserProfile.java:47)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.3.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.3.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.3.0:55)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6776)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Application terminated.

User Profile class activity :

public class UserProfile extends AppCompatActivity {
    FirebaseAuth auth;
    DatabaseReference reff;
    TextView a,b,c,d;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_profile);

        auth = FirebaseAuth.getInstance();

        a=findViewById(R.id.fullnameTxt);
        b=findViewById(R.id.fulladdresTxt);
        c=findViewById(R.id.fullphonTxt);
        d=findViewById(R.id.fullemailText);

        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

        if (user != null) {
            String uid = user.getUid();
            Toast.makeText(UserProfile.this,  uid,
                    Toast.LENGTH_SHORT).show();

               reff= FirebaseDatabase.getInstance().getReference("user/"+uid);
               reff.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String name = dataSnapshot.child("username").getValue().toString();
                        String add = dataSnapshot.child("userphone").getValue().toString();
                        String phon = dataSnapshot.child("useraddress").getValue().toString();
                        String e = dataSnapshot.child("useremail").getValue().toString();

                        a.setText(name);
                        b.setText(add);
                        d.setText(phon);
                        c.setText(e);

                    }
                                              @Override
                    public void onCancelled(DatabaseError databaseError) {
                        System.out.println("The read failed: " + databaseError.getCode());
                    }
                });
        }



        }
}

l dont have any issues while l am getting user uid and display in Toast. But when i use uid in database i will get the error above .

Database structure

enter image description here

Author:Ali Ghassan,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/62341548/android-studio-firebase-auth-uid-using-in-database
yy