olajide
olajide

Reputation: 967

Firebase is returning null instead of data

I want to retrieve saved data in firebase real-time database, but the result is always null.

Below is the code.

    try{
        mDatabase = FirebaseDatabase.getInstance().getReference();
        myBal.setText("0");
        mDatabase.child("users").child(mUserId).child("claim").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String amount  = dataSnapshot.child("satoshi").getValue(String.class);
                myBal.setText("satoshi: "+amount);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

Below is my database structure...

enter image description here

Thanks in advance!

Upvotes: 1

Views: 3699

Answers (1)

Lewis McGeary
Lewis McGeary

Reputation: 7922

The problem is you're not actually reaching the "satoshi" node.

The ValueEventListener on the "claim" node returns a DataSnapshot representing the "claim" node. There is no "satoshi" child on the "claim" node, the only child is "-Kbvm.....".

You would need to either have the "-Kbvm....." key already and use that to properly follow the path to "satoshi" or else use a ChildEventListener on the "claim" node to get the children of "claim" rather than "claim" itself.

Upvotes: 3

Related Questions