null pointer exception while retrieving from firebase [duplicate]

天大地大妈咪最大 提交于 2020-01-30 13:09:37

问题


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());
                // ...
            }
        });

    }
}


回答1:


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


来源:https://stackoverflow.com/questions/59718958/null-pointer-exception-while-retrieving-from-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!