0

I have an app that was writing and reading perfectly to and from Firebase. I just added in a timestamp, which is converted from a string (see code below):

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            Date parsedDate = dateFormat.parse(eventDate);
            Timestamp timestamp1 = new java.sql.Timestamp(parsedDate.getTime());
            timestamp2 = timestamp1;
        } catch(Exception e) {

        }

My problem is that when I try to read from the database, I get the following error:

"com.google.firebase.database.DatabaseException: Class java.sql.Timestamp does not define a no-argument constructor."

This error comes when I try to read the data using a data snapshot. See my code below:

eventRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {
                    Event event = eventSnapshot.getValue(Event.class);
                    events.add(event);
                }
                adapter.notifyDataSetChanged();
            }

The error appears to be at the following line in this code:

Event event = eventSnapshot.getValue(Event.class);

Does anyone know how to solve this?

UPDATE: Code for my event class below:

import java.sql.Timestamp;

public class Event {

    public String id;
    public String name;
    public String society;
    public String date;
    public String time;
    public String location;
    public String details;
    public String nameLower;
    public Timestamp timestamp;

    public Event() {

    }

    //required default constructor
    public Event(String id, String name, String society, String date, String time, String location, String details, String nameLower, Timestamp timestamp) {
        this.id = id;
        this.name = name;
        this.society = society;
        this.date = date;
        this.time = time;
        this.location = location;
        this.details = details;
        this.nameLower = nameLower;
        this.timestamp = timestamp;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSociety() {
        return society;
    }

    public void setSociety(String society) {
        this.society = society;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public String getNameLower() {
        return nameLower;
    }

    public void setNameLower(String nameLower) {
        this.nameLower = nameLower;
    }

    public Timestamp getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Timestamp timestamp) {
        this.timestamp = timestamp;
    }
}
minikate
  • 305
  • 1
  • 6
  • 16
  • What exactly is `Event`? Edit the question to show that class. And does it really make sense to use a `java.sql.Timestamp` here? Usually you store a data as a long integer or Date type object. – Doug Stevenson Feb 17 '19 at 13:58
  • Event is just a class I have made, which stores information about events on the database. I added timestamp because I wish to create a Cloud Function script that will delete old events - see this answer which uses a timestamp to do this https://stackoverflow.com/questions/32004582/delete-firebase-data-older-than-2-hours - I have also added the code for my Event class above as requested :) – minikate Feb 17 '19 at 14:14
  • That other question is about JavaScript, and it doesn't make reference to `java.sql.Timestamp`, so that doesn't help me understand why you need to use it here. As I said, you should probably use a Date or long. – Doug Stevenson Feb 17 '19 at 14:16
  • The question uses the timestamp field in the database to figure out which code to delete. ``var old = ref.orderByChild('timestamp').endAt(cutoff).limitToLast(1);`` Could I just store the timestamp as a string in my database? – minikate Feb 17 '19 at 14:21
  • 3
    The `java.sql.Timestamp` type has no relation to Firebase. It's for SQL databases accessed by JDBC. In Realtime Database, timestamps are just long integers. – Doug Stevenson Feb 17 '19 at 14:22
  • Okay, that helps me, thank you :) – minikate Feb 17 '19 at 14:23
  • Many Android library classes will not meet the requirements from Firebase, and you'll have to write your own serialization/deserialization to exchange the necessary information with the database. See https://stackoverflow.com/questions/50894716/how-to-add-no-argument-constructor-of-inbuilt-class-for-firebase-database-except – Frank van Puffelen Feb 17 '19 at 15:13

0 Answers0