jean d'arme
jean d'arme

Reputation: 4353

Get Timestamp from custom class

I've searched for two hours and I'm astounded there is no simple way for retrieving Timestamp from Firebase using custom class.

This is how document I'm retrieving looks like:

enter image description here

I have for this document simple custom class:

import com.google.firebase.Timestamp;

import java.util.List;

public class CapObj {
    private String content;
    private List<String> tags;
    private Timestamp created_at;

    public CapObj () {}

    public CapObj (String content, List<String> tags, Timestamp created_at){
        this.content = content;
        this.tags = tags;
        this.created_at = created_at;
    }

    public String getContent() {
        return content;
    }

    public List<String> getTags() {
        return tags;
    }

    public Timestamp getCreatedAt() {
        return created_at;
    }
}

and I populate my list with this method:

private void populateDatasetFromQuery(Task<QuerySnapshot> task){

    ArrayList<Caption> results = new ArrayList<>();
    for (QueryDocumentSnapshot document : task.getResult()) {

        CapObj capObj = document.toObject(Caption.class);
        results.add(caption);
    }
    capObjList = new ArrayList<>(results);
}

So the problem is that when I try to use capObj.getContent() I get content, but when I do capObj.getCreatedAt().toDate() I get:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Date com.google.firebase.Timestamp.toDate()' on a null object reference. even though the object I got from Firebase query looks like this:

{created_at=Timestamp(seconds=1583017200, nanoseconds=0), content=Travel 2, tags=[travel]}

To me it seems that created_at is not populated when object is created. What am I doing wrong here?

Upvotes: 0

Views: 93

Answers (2)

Deepak
Deepak

Reputation: 157

Instead of timestamp, you can use timeinmillies and you also need to setter method: You can do like this:

import java.util.List;

public class CapObj {
@SerializedName("content")
private String content;
@SerializedName("tags")
private List<String> tags;
@SerializedName("created_at")
private long created_at;

public CapObj () {}

public CapObj (String content, List<String> tags){
    this.content = content;
    this.tags = tags;
    this.created_at = Calendar.getInstance().getTimeInMillis();
}


public void setContent(String content) {
    this.content = content;
}

public void setTags(List<String> tags) {
    this.tags = tags;
}

public long getCreated_at() {
    return created_at;
}

public void setCreated_at(long created_at) {
    this.created_at = created_at;
}


public String getContent() {
    return content;
}

public List<String> getTags() {
    return tags;
}


}

after getting data you can convert long to calendar date-time using below method:

Date getDateFromLong(long timeInMillis){
 Calendar time_date=Calendar.getInstance();
 time_date.setTimeInMillis(timeInMillis);
 return time_date.getTime();
  }

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317692

Firestore doesn't know how to map the created_at field into your object. What you should do is create a public setter method that matches the field:

public void setCreated_at(Timestamp timestamp) {
    created_at = timestamp;
}

Since you're using underscores in the field name, that makes for an ugly setter name (as is the convention for Java POJO), so you might want to give it a better name, and annotate it with PropertyName so that Firestore knows which field should be used to call that method:

@PropertyName("created_at")
public void setCreatedAt(Timestamp timestamp) {
    created_at = timestamp;
}

The general convention for all POJOs is to have matching public getters and setters with the same property name (e.g. getFoo/setFoo).

Upvotes: 2

Related Questions