-1

Unable to fetch data from my database all permissions are public and dependencies included. I am trying to print data to logcat, receiving NullPointerException

MainActivity.java


import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {

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

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference();
        myRef.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String co2=dataSnapshot.child("co2").getValue().toString();
                String humi=dataSnapshot.child("humi").getValue().toString();
                String temp=dataSnapshot.child("temp").getValue().toString();
                Log.i("kkk","hhh"+ co2 +" "+humi +" "+temp);
                // ...
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                // Getting Post failed, log a message
                Log.w("p", "loadPost:onCancelled", databaseError.toException());
                // ...
            }
        });

    }
}

**My db Structure**

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
Sai YuvaTeja
  • 73
  • 1
  • 10

1 Answers1

2

Under your root reference, there are multiple objects. In order to get those objects, you need to iterate over the DataSnapshot object using getChildren() method, like in the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            double co2 = ds.child("co2").getValue(Double.class);
            long humi = ds.child("humi").getValue(String.class);
            long temp = ds.child("temp").getValue(String.class);
            Log.d(TAG, co2 + " / " + humi + " / " + temp);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
rootRef.addListenerForSingleValueEvent(valueEventListener);

The result in your logcat will be:

1007.87401... / 70 / 24
1259.84251... / 63 / 26
1511.81102... / 63 / 26
//The other records
Alex Mamo
  • 91,677
  • 13
  • 105
  • 138
  • Alex Mamo how did you figure this out any docs please share .I was unable to find this in documentation – Sai YuvaTeja Jan 13 '20 at 17:25
  • Here are the [docs](https://firebase.google.com/docs/database/android/read-and-write). Regarding how could I figure this out, was to see that there more than one children under your root. – Alex Mamo Jan 13 '20 at 17:29