-1

how can read value from firebase to Android?

I had write value to firebase, but how can read value to android?

my code

private String userid;
private TextView username;
username = findViewById(R.id.username);


 firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                UserModel userModel = dataSnapshot.getValue(UserModel.class);
                username.setText(userModel.getUsername());
                //publisher.setText(userModel.getUsername());
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

But I got the error code

Process: com.luvtas.campingau, PID: 23997

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.luvtas.campingau/com.luvtas.campingau.Ui.PostActivity}: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()

where has problem?

and because I want to get current userid from firebase to get user name and profile image.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    `userid` is null, from the code you've posted, i can't see where you've assigned it a value, so it makes sense that your app would crash – a_local_nobody Mar 01 '21 at 13:15
  • As a_local_nobody mentioned in his comment, the `userid` is null. Are you sure the user is authenticated? – Alex Mamo Mar 01 '21 at 13:40
  • @a_local_nobody because I want to get current userid from firebase to get user name and profile image – ToRo Tseng Mar 01 '21 at 13:50
  • @AlexMamo because I want to get current userid from firebase to get user name and profile image – ToRo Tseng Mar 01 '21 at 13:51
  • https://stackoverflow.com/questions/45813053/cant-pass-null-for-argument-pathstring-in-child-in-firebase-database/45813675 you can go through this link.It was posted by someone 3 years back but error is quite similar.And I was somehow able to solve using this. – Aarti Mar 01 '21 at 13:23

1 Answers1

1

It seems your String "userid" is null that is why you are getting an error so you need to pass the actual userid in the string userid to do this you can run the following code:

private String userid;
private TextView username;
username = findViewById(R.id.username);

    firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    userid = firebaseUser.getUid();
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            UserModel userModel = dataSnapshot.getValue(UserModel.class);
            username.setText(userModel.getUsername());
            //publisher.setText(userModel.getUsername());
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
Brandon
  • 146
  • 8
  • thx bro, I have other question, if I want to get image url. how can modity this ? use private Context context? Glide.with(context).load(userModel.getUserimage()); – ToRo Tseng Mar 01 '21 at 14:19
  • just say In Activity: Glide.with(getApplicationContext()).load(userModel.getUserimage()).into("whatever view you want to load image"). In Fragment: Glide.with(getContext()).load(userModel.getUserimage()).into("whatever view you want to load image"). – Brandon Mar 01 '21 at 19:41