Huzaifa
Huzaifa

Reputation: 532

How to get a custom object from Firebase Firestore?

I am using a custom class to store and retrieve data from a Firestore database. Here's my custom class:

public class SentMessage {
    private String sender;
    private String receiver;
    private String message;
    private String time;
    private boolean isImage;
    private boolean isArchived;
    @ServerTimestamp
    private Date timestamp;
    private String userId;

    public SentMessage(String sender, String receiver, String message, String time, boolean isImage, String userId) {
        this.sender = sender;
        this.receiver = receiver;
        this.message = message;
        this.time = time;
        this.isImage = isImage;
        this.isArchived = false;
        this.userId = userId;
    }
}

My data in firesotre looks like this:

enter image description here

I am using the following query:

database.collection("Sent Messages").whereEqualTo("Sender", email).whereEqualTo("isArchived", false).get()
                                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                                    @Override
                                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                                        for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                                            Log.d("cs50", "documentSnapshot.toObject(SentMessage.class).getMessage() = " + documentSnapshot.toObject(SentMessage.class).getMessage());
                                            Log.d("cs50", "documentSnapshot.getString(\"Message\") = " + documentSnapshot.getString("Message"));
                                            if (documentSnapshot.toObject(SentMessage.class) == null) {
                                                Log.d("cs50", "documentSnapshot.toObject(SentMessage.class) is null");
                                            } else {
                                                Log.d("cs50", "documentSnapshot.toObject(SentMessage.class) is not null");
                                            }
                                            SentMessage tempSentMessage = new SentMessage(documentSnapshot.getString("Sender"), documentSnapshot.getString("Receiver"), documentSnapshot.getString("Message"), documentSnapshot.getString("Time"), documentSnapshot.getBoolean("isImage"), documentSnapshot.getString("userId"));
                                            Log.d("cs50", "tempSentMessage.getMessage() = " + tempSentMessage.getMessage());
                                            sentMessages.add(documentSnapshot.toObject(SentMessage.class));
                                        }
                                }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Log.e("cs50", "Error getting sent messages", e);
                            }
                        });

Here's my logcat output:

2020-09-23 00:02:27.395 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class).getMessage() = null
2020-09-23 00:02:27.395 4465-4465/com.example.treeapp D/cs50: documentSnapshot.getString("Message") = Do
2020-09-23 00:02:27.402 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class) is not null
2020-09-23 00:02:27.404 4465-4465/com.example.treeapp D/cs50: tempSentMessage.getMessage() = Do

2020-09-23 00:02:27.417 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class).getMessage() = null
2020-09-23 00:02:27.418 4465-4465/com.example.treeapp D/cs50: documentSnapshot.getString("Message") = Just
2020-09-23 00:02:27.424 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class) is not null
2020-09-23 00:02:27.425 4465-4465/com.example.treeapp D/cs50: tempSentMessage.getMessage() = Just

2020-09-23 00:02:27.436 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class).getMessage() = null
2020-09-23 00:02:27.436 4465-4465/com.example.treeapp D/cs50: documentSnapshot.getString("Message") = Cube
2020-09-23 00:02:27.440 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class) is not null
2020-09-23 00:02:27.442 4465-4465/com.example.treeapp D/cs50: tempSentMessage.getMessage() = Cube

2020-09-23 00:02:27.450 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class).getMessage() = null
2020-09-23 00:02:27.450 4465-4465/com.example.treeapp D/cs50: documentSnapshot.getString("Message") = Dump
2020-09-23 00:02:27.454 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class) is not null
2020-09-23 00:02:27.455 4465-4465/com.example.treeapp D/cs50: tempSentMessage.getMessage() = Dump

2020-09-23 00:02:27.462 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class).getMessage() = null
2020-09-23 00:02:27.463 4465-4465/com.example.treeapp D/cs50: documentSnapshot.getString("Message") = Sell
2020-09-23 00:02:27.466 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class) is not null
2020-09-23 00:02:27.467 4465-4465/com.example.treeapp D/cs50: tempSentMessage.getMessage() = Sell

2020-09-23 00:02:27.474 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class).getMessage() = null
2020-09-23 00:02:27.475 4465-4465/com.example.treeapp D/cs50: documentSnapshot.getString("Message") = Pixel
2020-09-23 00:02:27.478 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class) is not null
2020-09-23 00:02:27.480 4465-4465/com.example.treeapp D/cs50: tempSentMessage.getMessage() = Pixel

2020-09-23 00:02:27.486 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class).getMessage() = null
2020-09-23 00:02:27.487 4465-4465/com.example.treeapp D/cs50: documentSnapshot.getString("Message") = Bill
2020-09-23 00:02:27.490 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class) is not null
2020-09-23 00:02:27.491 4465-4465/com.example.treeapp D/cs50: tempSentMessage.getMessage() = Bill

2020-09-23 00:02:27.498 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class).getMessage() = null
2020-09-23 00:02:27.498 4465-4465/com.example.treeapp D/cs50: documentSnapshot.getString("Message") = Can
2020-09-23 00:02:27.501 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class) is not null
2020-09-23 00:02:27.503 4465-4465/com.example.treeapp D/cs50: tempSentMessage.getMessage() = Can

2020-09-23 00:02:27.509 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class).getMessage() = null
2020-09-23 00:02:27.510 4465-4465/com.example.treeapp D/cs50: documentSnapshot.getString("Message") = Last party
2020-09-23 00:02:27.513 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class) is not null
2020-09-23 00:02:27.515 4465-4465/com.example.treeapp D/cs50: tempSentMessage.getMessage() = Last party

2020-09-23 00:02:27.520 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class).getMessage() = null
2020-09-23 00:02:27.520 4465-4465/com.example.treeapp D/cs50: documentSnapshot.getString("Message") = Do
2020-09-23 00:02:27.522 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class) is not null
2020-09-23 00:02:27.524 4465-4465/com.example.treeapp D/cs50: tempSentMessage.getMessage() = Do

2020-09-23 00:02:27.529 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class).getMessage() = null
2020-09-23 00:02:27.529 4465-4465/com.example.treeapp D/cs50: documentSnapshot.getString("Message") = Some
2020-09-23 00:02:27.531 4465-4465/com.example.treeapp D/cs50: documentSnapshot.toObject(SentMessage.class) is not null
2020-09-23 00:02:27.533 4465-4465/com.example.treeapp D/cs50: tempSentMessage.getMessage() = Some

I have also tried it with a different field (Time), and I get a similar output.

Why is it that documentSnapshot.toObject(SentMessage.class).getMessage() is null, while documentSnapshot.toObject(SentMessage.class) is not null? I have confirmed that my custom class is working properly because tempSentMessage.getMessage() is not null.

What I later do in the query onSuccess() is check for (SentMessage sentMessage : sentMessages) if (sentMessage.getReceiver().equals(peerEmail)), among other things, which gives me an error because sentMessage, which is an element of sentMessages, is null. That's where I got the original error from, and then I added the log statements.

Upvotes: 1

Views: 385

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317552

Your document data does not correctly match your class definition for the "Message" field.

When you have a Java getter called "getMessage", the Firestore SDK will attempt to fill that with a field called "message" (with a lowercase 'm'). Your field is "Message" with a capital M. This is the way that the JavaBeans spec maps values to Java objects. It is not forgiving about case - they must match exactly.

You should either change the name of your field to "message" to suit your Java class, or use @PropertyName to annotate the object property with the name of the document field to use that's different than what's required by the JavaBeans spec.

Upvotes: 1

Related Questions