Java – How do I use a constructor that already exists?

How do I use a constructor that already exists?… here is a solution to the problem.

How do I use a constructor that already exists?

I’m building a simple app for child using Firebase, but I get this error often:

Android Firebase Database exception: not define a no-argument constructor

I have an Activity class and two other helper classes, HomeActivities and OutsideActivities. Here is my class code:

public class Activities {
    private String type;
    private int count;

public Activities() { }

public Activities(String type, int count) {
        this.type = type;
        this.count = count;
    }

public String getType() {
        return type;
    }

public void setType(String type) {
        this.type = type;
    }

public int getCount() {
        return count;
    }

public void setCount(int count) {
        this.count = count;
    }

class HomeActivities {
        private String name;
        private int noOfChildren;

HomeActivities() { }

public HomeActivities(String name, int noOfChildren) {
            this.name = name;
            this.noOfChildren = noOfChildren;
        }

public String getName() {
            return name;
        }

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

public int getNoOfChildren() {
            return noOfChildren;
        }

public void setNoOfChildren(int noOfChildren) {
            this.noOfChildren = noOfChildren;
        }
    }

class OutsideActivities {
        private String name;
        private int noOfChildren;

public OutsideActivities() { }

public OutsideActivities(String name, int noOfChildren) {
            this.name = name;
            this.noOfChildren = noOfChildren;
        }

public String getName() {
            return name;
        }

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

public int getNoOfChildren() {
            return noOfChildren;
        }

public void setNoOfChildren(int noOfChildren) {
            this.noOfChildren = noOfChildren;
        }
    }
}

Check out all constructors. I get this error even though I have defined the correct constructor in each class. How to solve this problem? Please help me.

Solution

There are two ways to eliminate this error.

  1. Set the two inner classes to static.

  2. Make the two inner classes independent (each in its own separate file).

That’s it!

Related Problems and Solutions