1

This is the logcat:

java.lang.IllegalArgumentException: method group.onservice.onservice.GetData.setTimestamp argument 1 has type java.sql.Timestamp, got java.util.Date at java.lang.reflect.Method.invoke(Native Method) at com.google.android.gms.internal.zzevb$zza.zza(Unknown Source:134) at com.google.android.gms.internal.zzevb.zza(Unknown Source:437) at com.google.android.gms.internal.zzevb.zza(Unknown Source:2) at com.google.firebase.firestore.DocumentSnapshot.toObject(Unknown Source:9) at group.onservice.onservice.ProfileActivity$2.onEvent(ProfileActivity.java:155) at group.onservice.onservice.ProfileActivity$2.onEvent(ProfileActivity.java:146) at com.google.firebase.firestore.zzi.onEvent(Unknown Source:16) at com.google.android.gms.internal.zzevc.zza(Unknown Source:6) at com.google.android.gms.internal.zzevd.run(Unknown Source:6) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

This is the code where the error occurred:

firebaseFirestore.collection("Posts").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

                for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                    if (doc.getType() == DocumentChange.Type.ADDED) {
                        AdPost adPost = doc.getDocument().toObject(AdPost.class);
                        ad_list.add(adPost);

                        adRecyclerAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

I didn't actually knew how to program this, so I searched in some forums and vids for how to do it (I'm kinda new at programming), and the thing is: I saw everyone using the same exact code, and it worked for all of them, except for me. Because of that, no one reported any kind of error. Can someone help me to understand what is wrong with my code?

PS: the exact line where the error is appointed is this:

AdPost adPost = doc.getDocument().toObject(AdPost.class);

AdPost.class:

package group.onservice.onservice;

import java.sql.Timestamp;

public class AdPost {

    String des, image_url, titu, user_id, valo;
    Timestamp timestamp;

    public AdPost() {

    }

    public AdPost(String des, String image_url, String titu, String user_id, String valo, Timestamp timestamp) {
        this.des = des;
        this.image_url = image_url;
        this.titu = titu;
        this.user_id = user_id;
        this.valo = valo;
        this.timestamp = timestamp;
    }

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }

    public String getImage_url() {
        return image_url;
    }

    public void setImage_url(String image_url) {
        this.image_url = image_url;
    }

    public String getTitu() {
        return titu;
    }

    public void setTitu(String titu) {
        this.titu = titu;
    }

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getValo() {
        return valo;
    }

    public void setValo(String valo) {
        this.valo = valo;
    }

    public Timestamp getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Timestamp timestamp) {
        this.timestamp = timestamp;
    }

}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
nonutxd
  • 13
  • 3

1 Answers1

0

The problem in your code is that you are using for the timestamp, a Timestamp object and not a Date object. The Timestamp class is not a supported data type in Firestore. To solve this, I recommend you to see my answer from this post, where I have explained how to store the timestamp in Firebase real-time database using the Date class. You'll see that are two ways, one using a model class and the other using a map and FieldValue.serverTimestamp().

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193