首页 > 解决方案 > 从 firebase 检索数据时,getter 返回 null

问题描述

我正在尝试从 firebase 检索数据。我已成功建立 Firebase 数据库,但在检索操作期间,getter 方法返回空值,如何检索该信息?对象不为空,但 getter 返回空值。

public class ViewDatabase extends AppCompatActivity {

    public FirebaseDatabase firebaseDatabase;
    public DatabaseReference databaseReference;
    public ListView listView;
    public static final String TAG="ViewDatabase";
    //Oncreate
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_database);
        listView=findViewById(R.id.list);
        firebaseDatabase =FirebaseDatabase.getInstance();
        databaseReference=firebaseDatabase.getReference();
        databaseReference.addValueEventListener(new ValueEventListener()
        {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot)
            {
                showData(dataSnapshot);
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) { }
        });
    }
    //Retriving data
    private void showData(DataSnapshot dataSnapshot) {
        ArrayList<String> array = new ArrayList<String>();
        for (DataSnapshot ds : dataSnapshot.getChildren()) {
            User user = ds.getValue(User.class);

            if (user != null) {
                Log.i(TAG, "user is not null" );

            } else {
                Log.i(TAG, "user is  null" );

            }
            if(user.getAddress()!=null){
              Log.i(TAG, "address is not null" );
            }
            else{
           Log.i(TAG, "address is  null" );

             }
            array = new ArrayList<String>();
            array.add(user.getAddress());
            array.add(user.getDate());
            array.add(user.getEmail());
            array.add(user.getFname());
            array.add(user.getGender());
            array.add(user.getZone());
            array.add(user.getLname());
            array.add(user.getPhone());


        }
        ArrayAdapter adapter = new ArrayAdapter(this, layout.simple_list_item_1, array);
        listView.setAdapter(adapter);

    }

}

我究竟做错了什么?

//JSON EXPORT
{"User" : {
"-LIN7aCLwUo7fdjSm5ij" : {
  "address" : "jagraon",
  "date" : "2018/7/27",
  "email" : "gurjaps00@gmail.com",
  "fname" : "Gurjap",
  "id" : "-LIN7aCLwUo7fdjSm5ij",
  "lname" : "Singh",
  "phone" : "8727050501",
  "zone" : "CARDIO ZONE"
},
"-LIN7iFVzA8eVShCSmwu" : {
  "address" : "jagraon",
  "date" : "2018/7/25",
  "email" : "livtaran@gmail.com",
  "fname" : "Gurlivtaran",
  "id" : "-LIN7iFVzA8eVShCSmwu",
  "lname" : "Singh",
  "phone" : "8725000740",
  "zone" : "STRENTH ZONE"
},
"-LIjA7Kox5dtN8jg9ywY" : {
  "address" : "chuuu",
  "date" : "Select Expiry Date",
  "email" : "cbjj@tuj.dhhu",
  "fname" : "dhhxc",
  "lname" : "cvhh",
  "phone" : "9856533565",
  "zone" : "CARDIO ZONE"}

} }

这是从 firebase 完成的 JSON 导出。

标签: androidfirebasefirebase-realtime-databasenullpointerexception

解决方案


您正在从数据库中的错误级别读取。

您正在从根目录读取,然后直接在其下遍历子代。这意味着您获得了快照,/User并且您正在尝试从中读取User.class。那是行不通的,因为 , 下没有属性address,name等。这些属性在您的 JSON 中低一级,因此您的代码也需要遍历各个用户。/User

一个简单的修复:

private void showData(DataSnapshot dataSnapshot) {
    ArrayList<String> array = new ArrayList<String>();
    for (DataSnapshot ds : dataSnapshot.getChildren()) {
        System.out.println(ds.getKey());
        for (DataSnapshot userSnapshot : ds.getChildren()) {
            System.out.println(userSnapshot.getKey());
            User user = userSnapshot.getValue(User.class);
            ...

另一种选择(可能是更好的解决方案)可能是只读取/User节点:

firebaseDatabase =FirebaseDatabase.getInstance();
databaseReference=firebaseDatabase.getReference();
databaseReference.child("User").addValueEventListener(new ValueEventListener() {
    ...

推荐阅读