java – GetKey method returns null in dataSnapshot-ThrowExceptions

Exception or error:

I’m a beginner struggling to grasp Android Studio and I’m currently stuck on a problem. I’m trying to pull a read data from my Database in text view. When I use getKey method, it returns null even though there are values in the DB. Note I am trying to display “name” (222) in a text view box.
Here is my code

How to solve:

If you want to retrieve the name, then try the following:

        ref = FirebaseDatabase.getInstance().getReference("Users").child(currentuser).child("Teams");
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

               for(DataSnapshot ds : dataSnapshot.getChildren()){

                String name = ds.child("name").getValue(String.class);
                a = (TextView)findViewById(R.id.test1);
                a.setText(name);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
              throw databaseError.toException();
            }


        });

First add a reference to node Teams then iterate and retrieve the value of name

Leave a Reply

Your email address will not be published. Required fields are marked *