【问题标题】:Null object reference in Firebase result loopFirebase 结果循环中的空对象引用
【发布时间】:2017-12-07 13:35:03
【问题描述】:

我想在 Firestore 中接收所有挑战并循环遍历结果以将其添加到 ArrayList。

db.collection("challenges")
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot document : task.getResult()) {
                        if (document.exists()) {
                            Log.d(TAG, document.getId() + " => " + document.getData());
                    Error-> Challenge challenge = document.toObject(Challenge.class);
                            Log.d(TAG, challenge.getUid() + " => " + challenge.getText());
                            challengeList.add(document.getData().toString());
                        }
                    }

                    challengeListView.setAdapter(challengeArrayAdapter);
                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                }
            }
        });

错误是:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[])' on a null object reference

行:

Challenge challenge = document.toObject(Challenge.class);

Log.d(TAG, document.getId() + " =&gt; " + document.getData()); 的日志 正在工作。

参考:https://firebase.google.com/docs/firestore/query-data/get-data#get_all_documents_in_a_collection

这是我的挑战课:

public class Challenge {

    private Date createdAt;
    private Date changedAt;
    private String uid;
    private String text;
    private double longitude;
    private double latitude;

    public Challenge(String uid, String text, double longitude, double latitude) {
        Date currentDate = new Date();
        this.createdAt = currentDate;
        this.changedAt = currentDate;
        this.uid = uid;
        this.text = text;
        this.longitude = longitude;
        this.latitude = latitude; }

    public Date getCreatedAt() { return createdAt; }

    public Date getChangedAt() { return changedAt; }

    public String getUid() { return uid; }

    public String getText() { return text; }

    public double getLongitude() { return longitude; }

    public double getLatitude() { return latitude;}
}

【问题讨论】:

    标签: java android firebase google-cloud-firestore


    【解决方案1】:

    你需要更改这行代码:

    Challenge challenge = document.toObject(Challenge.class);
    

    Map<String, Object> map = task.getResult().getData());
    

    要对单个文档使用toObject()方法,请使用以下代码:

    DocumentReference docRef = db.collection("challenges").document("yourDocument");
    docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            Challenge challenge = documentSnapshot.toObject(Challenge.class);
        }
    });
    

    错误是因为您的模型类。你的代码看起来不错。在模型类中添加无参数构造函数。保留该代码或使用Map&lt;String, Object&gt;,您的问题将得到解决。

    要添加无参数构造函数,请在模型类中添加以下代码行:

    public Challenge() {} //Needed for Firebase
    

    在声明类变量之后。

    【讨论】:

    • 但我想查询挑战中的所有数据。有没有办法在不选择特定文档的情况下做到这一点?
    • 如果要单个文档,需要使用上面的代码。如果您想要所有文档,则需要遍历整个集合。
    • 但我认为我这样做就像使用 firebase 文档来查询集合的所有数据
    • document.toObject(Challenge.class) 返回什么?空?
    • 请分享您的Challenge 班级。
    【解决方案2】:

    我知道为时已晚,但在我的情况下它可能对某些人有所帮助,我的对象是

    public class RFQ {
        public String content,email,receiverKey,productKey,companyKey;
        public Sender sender;
        public Long createAt;
    
        public RFQ() {
        }
    
        public RFQ(String content, String email, String receiverKey, String productKey, String companyKey, Sender sender, Long createAt) {
            this.content = content;
            this.email = email;
            this.receiverKey = receiverKey;
            this.productKey = productKey;
            this.companyKey = companyKey;
            this.sender = sender;
            this.createAt = createAt;
        }
    
        public String getContent() {
            return content;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public Long getCreateAt() {
            return createAt;
        }
    
        public void setCreateAt(Long createAt) {
            this.createAt = createAt;
        }
    
        public String getReceiverKey() {
            return receiverKey;
        }
    
        public void setReceiverKey(String receiverKey) {
            this.receiverKey = receiverKey;
        }
    
        public String getProductKey() {
            return productKey;
        }
    
        public void setProductKey(String productKey) {
            this.productKey = productKey;
        }
    
        public String getCompanyKey() {
            return companyKey;
        }
    
        public void setCompanyKey(String companyKey) {
            this.companyKey = companyKey;
        }
    
        public Sender getSender() {
            return sender;
        }
    
        public void setSender(Sender sender) {
            this.sender = sender;
        }
    }
    

    这里一切正常,但我仍然收到错误,这是由于我的 Sender 类中的 Sender 类我错过了放置非参数构造函数。

    【讨论】:

      【解决方案3】:

      兄弟,您需要在挑战模型类中添加默认构造函数,因为 Firebase 需要它

      public Challenge() {} //添加这一行

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-07
        • 1970-01-01
        • 2017-08-04
        • 2011-06-19
        • 1970-01-01
        相关资源
        最近更新 更多