从Firebase检索数据时,getter返回null

时间:2018-07-27 08:19:00

标签: android firebase firebase-realtime-database nullpointerexception

我正在尝试从Firebase检索数据。我已经成功建立了Firebase数据库,但是在检索操作期间,getter方法返回的是空值,如何检索该信息?对象不是null,但是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导出。

1 个答案:

答案 0 :(得分:0)

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

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

一个简单的解决方法:

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() {
    ...