Skip to content Skip to sidebar Skip to footer

Firebase Unable To Retrieve Data Using Class

I have some fully functional lines of code retrieving each single data, but failed to retrieve them using class. Eg: These lines are working perfectly. double value = (double) ds.c

Solution 1:

You are getting the following error:

FATAL EXCEPTION: ... does not define a no-argument constructor

Because your Round class does not define a no-argument constructor.

JavaBeans require a no-argument constructor to be present in the model class.

In Java, when a class has no constructors at all, there is a default no-argument constructor automatically added by the Java compiler. The moment you define any constructor in the class, the default no-argument constructor goes away.

In your code, your Round class defines such a constructor that contains five arguments:

public Round(double player1score, double player2score, double player3score, double player4score, long point) {}

As long as this constructor is present and you don't define a no-argument constructor, that class will not have one.

To solve this, you either remove that constructor from the class, or manually add a no-argument constructor as shown below:

public Round() {}

When the Firebase Realtime database SDK deserializes objects that are coming from the database, it requires that any objects in use, to have this constructor, so it can use it to instantiate the object. Fields in the objects are set by using public setter methods or direct access to public members.

Your Round class dosen't have a public no-arg constructor, the SDK doesn't really know how to create an instance of it. So it is mandatory to have it.

Also please note that setters and getter are not required. Setters are always optional because if there is no setter for a JSON property, the Firebase client will set the value directly onto the field. A constructor-with-arguments is also not required. Both are idiomatic and there are good cases to have classes without them. If you make the fields public, the getters are optional too.


Post a Comment for "Firebase Unable To Retrieve Data Using Class"