【问题标题】:Can't access value from a nested Hashmap无法从嵌套的 Hashmap 访问值
【发布时间】:2018-05-27 22:32:56
【问题描述】:

从 Firebase 检索这组数据,我无法获取 user_description 部分的值。

Firebase 数据

final DatabaseReference fb = FirebaseDatabase.getInstance().getReference();

    DatabaseReference complaint = fb.child("complaints");
    complaint.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot data) {

            for (DataSnapshot child : data.getChildren()) {
                Complaint complaint = child.getValue(Complaint.class);

                String key = complaint.complaint_description.keySet().toString();
                String desc= complaint.complaint_description.get(key).get("user_description");
                Log.v("ComplaintFragment", desc);

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.v("ComplaintFragment", databaseError);

        }

收到错误:

    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.get(java.lang.Object)' on a null object reference

这里是投诉类

public class Complaint implements Serializable  {

HashMap<String, HashMap<String, String>> complaint_description;
String complaint_id;
String complaint_subject;
String facility_name;
String facility_type;
boolean fixed;
int join_staff;
int join_student;
int join_total;
String location_area;
String location_floor;
String note;
boolean sent;
String user_id;

public Complaint() {
}

public Complaint(HashMap<String, HashMap<String, String>> complaint_description, String complaint_id, String complaint_subject, String facility_name, String facility_type, boolean fixed, int join_staff, int join_student, int join_total, String location_area, String location_floor, String note, boolean sent, String user_id) {

   //initialization
}

//set and get methods here

}

我该怎么办? 谢谢

【问题讨论】:

  • 如果你尝试使用Log.v("ComplaintFragment", key);,它会返回什么?
  • @AlexMamo 问题是 Log.v("ComplaintFragment", complaint.complaint_description.get("-LDXSyVDoMIT01vs9GTV")); 返回 {user_id=l5JtSqF9wgUfpUbSl6oKONO0poB3, user_description=wow} 但Log.v("ComplaintFragment", complaint.complaint_description.get("-LDXSyVDoMIT01vs9GTV").get("user_description"); 返回错误 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.get(java.lang.Object)' on a null object reference

标签: android firebase firebase-realtime-database hashmap


【解决方案1】:

因为你的数据库引用错误,所以你得到了 null

您正在访问您的主树节点投诉

DatabaseReference complaint = fb.child("complaints");

但您需要访问另外一个节点,即您的用户 ID

DatabaseReference complaint = fb.child("complaints").child(uid);

请记住,可以使用 FirebaseAuth 检索 uid

FirebaseAuth mAuth;

mAuth  = FirebaseAuth.getInstance();
String uid = mAuth.getCurrentUser().getUid();

让我知道这是否有效

【讨论】:

  • 嗨,我现在面临的问题是 Log.v("ComplaintFragment", complaint.complaint_description.get("-LDXSyVDoMIT01vs9GTV")); 返回 {user_id=l5JtSqF9wgUfpUbSl6oKONO0poB3, user_description=wow} 但 Log.v("ComplaintFragment", complaint.complaint_description.get("-LDXSyVDoMIT01vs9GTV").get("user_description"); 返回错误 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.get(java.lang.Object)' on a null object reference
  • 问题是对象 user_description 不在您的 Complaint.class 中,您需要添加它,因此当您尝试获取该值时,您没有得到任何对它的引用
  • 那么先生,我该怎么办?
  • 只需将 user_description 添加到您的 POJO 类 Complaint.class 中,然后当您检索值时将其存储在其中,请参阅stackoverflow.com/questions/42597084/…
  • 那是肯定的 :) 祝你有美好的一天!
猜你喜欢
  • 1970-01-01
  • 2019-09-28
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
  • 1970-01-01
  • 2019-04-27
  • 2011-02-16
  • 2021-07-19
相关资源
最近更新 更多