Firebase结果循环中的空对象引用

时间:2017-12-07 13:35:03

标签: java android firebase google-cloud-firestore

我希望在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() + " => " + 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;}
}

3 个答案:

答案 0 :(得分:4)

您需要更改以下代码行:

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);
    }
});

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

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

public Challenge() {} //Needed for Firebase
在类变量解密后

答案 1 :(得分:0)

我知道为时已晚,但对于我的对象可能对某些人有所帮助

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类导致的,我错过了放置非参数构造函数的时间。

答案 2 :(得分:0)

Bro,您需要在Challenge Model类中添加默认构造函数,这是Firebase所需的

  

public Challenge() {} //添加此行