Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase database always returns null

When reading from this firebase database it always returns null instead of the value I need.

I have tried pulling it in the form of a String with the String.class but its still null.

What I need to pull from the data base is just the int dose and the pName variable in this case would be dfdf

enter image description here

final FirebaseDatabase aDatabase = FirebaseDatabase.getInstance();
final DatabaseReference mDatabase = aDatabase.getReference();
DatabaseReference myRef =  aDatabase.getReference(pName).child("dose");

// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
       // This method is called once with the initial value and again
       // whenever data at this location is updated.
       Integer Value = dataSnapshot.getValue(Integer.class);
       String peter = Integer.toString(Value);
       count.setText(peter);
    }

    @Override
    public void onCancelled(DatabaseError error) {
        count.setText("E");
    }
});
like image 634
Joseph Thompson Avatar asked Dec 05 '25 04:12

Joseph Thompson


1 Answers

Try changing -

DatabaseReference myRef =  aDatabase.getReference(pName).child("dose");

to -

DatabaseReference myRef =  mDatabase.child(pName).child("dose");

you should be running on mDatabase not aDatabase and get the child not reference

And change the listener to

addListenerForSingleValueEvent

like image 200
Itay Feldman Avatar answered Dec 07 '25 19:12

Itay Feldman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!