I am building a simple app for children with Firebase and I'm constantly getting this error:
Android Firebase Database exception: not define a no-argument constructor
I have a class Activities and two other helper classes HomeActivities and OutsideActivities. This is my code for my classes:
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;
}
}
}
Please see all the constructors. Even if I already have defined the correct constructor in each class, I get this error. How to solve this? Please help me.
There are two methods in which you can get rid of this error.
Make both inner classes static.
Make both inner classes independent (each class in it's own separte file).
That's it!
Constructor of HomeActivities() has default visibility. public keyword is missed. Database ORM provider can't instantiate objects when the constructor is not public.
Should be:
class HomeActivities {
private String name;
private int noOfChildren;
public HomeActivities() { }
All other suggestions are valid as well. You should externalize classes in separate files.
Related
I have Fragments call CategoryFragments where I am adding a list name catList for that I define the variable in Class name CategoryMODEL and variable are
public class CategoryModel {
private String docID;
private String name;
private int noOfTests;
public CategoryModel() {
this.docID = docID;
this.name = name;
this.noOfTests = noOfTests;
}
public String getDocID() {
return docID;
}
public void setDocID(String docID) {
this.docID = docID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNoOfTests() {
return noOfTests;
}
public void setNoOfTests(int noOfTests) {
this.noOfTests = noOfTests;
}
}
but when I try to use add item in my catLIst I get error that Change Signature of CategoryModel
Because you have an empty constructor but inside your are init the values with themself, so create a constructor with params:
public CategoryModel(String docId, String name, int noOfTests) {
// Init your scope variables
}
public class Student {
private String name;
private long id;
private String grade;
private int[] test;
private int NUM_TESTS;
public Student(){
name="Un";
id=0;
grade="Un";
test=new int[0];
NUM_TESTS=5;
}
public Student(String x, long z) {
name=x;
id=z;
}
public void setName(String n) {
name=n;
}
public void setID(long i) {
id=i;
}
public void setGrade(String g) {
grade=g;
}
/*public void setTestScore(int t,int s) {
test=t;
test=s;
}
public int getTestScore(int) {
return test;
}*/
public int getNumTests() {
return NUM_TESTS;
}
public String getName() {
return name;
}
public long getID() {
return id;
}
public String getGrade() {
return grade;
}
public String toString() {
return getTestScore()+getNumTests()+getName()+getID()+getGrade();
}
/*public void calculateResult() {
int sum=0;
for (int t:test)sum+=t;
double average= 1.0t*sum/5;*/
}
}
Here is my code I have spaced out the places where I am having the issues. I am writing a Student subclass with subclasses undergrad and postgrad.
Here is the UML
I don't understand how to correctly implement testScore if it is not one of the variables? Nevermind the calculate result I'll fix that myself. I am also unsure if my constructors are accurate. All the students do five exams that's a constant
setTestScore(int t, int s)... I do recommend to use carefully chosen names (identifiers). For example if you just rename the parameters to: setTestScore(int testNumber, int score) you can be more familiar what should you inplement.
test = new int[0];isn't what you want. You want test = new int[NUM_TESTS]
Try to reconsider method setTestScore(int testNumber, int score)
first parameter is actually the index in the array of test and the second is the value.
So, your method should be something like this:
public void setTestScore(int testNumber, int score) {
test[testNumber] = score;
}
I just gave you some guidance for your own implementation...
First of all, It seems that Student class should be abstract. because each student is UnderGraduate or PostGraduate.
Secondly, you should extend the child classes from Student class.
I hope the below code be helpful:
abstract class Student {
private String name;
private long id;
private String grade;
private int[] test;
private final int NUM_TESTS = 5;
public Student(){
name = "UN";
id = 0;
grade = "UN";
test = new int[NUM_TESTS];
}
public Student(String name, long id){
this.name = name;
this.id = id;
}
#Override
public String toString() {
//TODO: write your desire toString method
return getNUM_TESTS()+getName()+getId()+getGrade();
}
abstract void claculateResult();
public int getTestScore(int testNumber){
if(testNumber >= NUM_TESTS)
return 0;
return test[testNumber];
}
public void setTestScore(int testNumber, int score){
if(testNumber >= NUM_TESTS)
return;
test[testNumber] = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public int[] getTest() {
return test;
}
public void setTest(int[] test) {
this.test = test;
}
public int getNUM_TESTS() {
return NUM_TESTS;
}
}
and the UnderGraduate class would be:
public class UnderGraduate extends Student{
public UnderGraduate(){
}
public UnderGraduate(String name, long id){
super();
}
#Override
void claculateResult() {
//TODO: DO whatever you want
}
}
remember that the PostGraduate class is same as UnderGraduate.
class person {
private int id ;
private String name;
private boolean gender;
public person() {
}
public AtomicLong getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
}
I want to create unique id in this class without using constructors and setters.
To construct a person instance, the field initializer will be copied into the constructor. Assuming that's okay, you could use an AtomicInteger and something like,
private static AtomicInteger ai = new AtomicInteger(0);
private int id = ai.incrementAndGet();
you could add:
private static int ID_GENERATOR = 0;
then, in the constructor, you will use:
public person() {
id = ID_GENERATOR++;
}
this is my current code to store rooms(it compiles fine) but in the UML there is a variable called addEquipment and there is also another class called Equipment to be defined. I'm having trouble wrapping my head around what I'm supposed to do with this. Am I supposed to create and call an object called Equipment? what goes in addEquipment?
public class Room {
//begin variable listing
private String name;
private int id;
private int capacity;
private String equipmentList;
//begins get methods for variables
public String getName(){
return name;
}
public int getID(){
return id;
}
public int getCapacity(){
return capacity;
}
public String getEquipmentList(){
return equipmentList;
}
// Set the variables
public void setName(String aName){
name=aName;
}
public void setID(int anID){
id=anID;
}
public void setCapacity(int aCapacity){
capacity=aCapacity;
}
public void setEquipmentList(String anEquipmentList){
equipmentList=anEquipmentList;
}
public String addEquipment(String newEquipment, String currentEquipment){
}
//Create room object
public Room(int capacity, String equipmentList) {
setCapacity(capacity);
setEquipmentList(equipmentList);
}
//Convert variables to string version of room
public String toString(){
return "Room "+name+", capacity: "+capacity+", equipment: "+getEquipmentList();
}
}
You can create a new class Equipment and modify your attribute equipmentList to be a List:
public class Equipment {
private String name;
public Equipment(String name) {
this.name = name;
}
}
public class Room {
//begin variable listing
private String name;
private int id;
private int capacity;
private List<Equipment> equipmentList = new ArrayList<Equipment>();
//begins get methods for variables
public String getName(){
return name;
}
public int getID(){
return id;
}
public int getCapacity(){
return capacity;
}
public List<Equipment> getEquipmentList(){
return equipmentList;
}
// Set the variables
public void setName(String aName){
name=aName;
}
public void setID(int anID){
id=anID;
}
public void setCapacity(int aCapacity){
capacity=aCapacity;
}
public void setEquipmentList(List<Equipment> anEquipmentList){
equipmentList=anEquipmentList;
}
public String addEquipment(String newEquipment, String currentEquipment){
Equipment oneEquipment = new Equipment(newEquipment);
equipmentList.add(oneEquipment);
}
//Create room object
public Room() {
setCapacity(capacity);
setEquipmentList(equipmentList);
}
//Convert variables to string version of room
public String toString(){
String capacity=String.valueOf(getCapacity());
String room = "Room "+name+", capacity: "+capacity+", equipment: "+getEquipmentList();
return room;
}
}
In the method addEquipment, you can create a new Equipment and add it to equipmentList, like code above.
An Equipment class could be anything. Lets assume the "Equipment"-class has a String called "name" as it's attribute
public class Equipment {
String name;
public Equipment( String name) {
this.name = name;
}
public String getName() {
return this.name
}
}
When you extend your Room class by the requested "addEquipment" method, you can do something like this.
public class Room {
... // Your code
private int equipmentIndex = 0;
private Equipment[] equipment = new Equipment[10]; // hold 10 Equipment objects
public void addEquipment( Equipment eq ) {
if ( equipmentIndex < 10 ) {
equipment[ equipmentIndex ] = eq;
equipmentIndex++;
System.out.println("Added new equipment: " + eq.getName());
} else {
System.out.println("The equipment " + eq.getName() + " was not added (array is full)");
}
}
}
Now when you call
room.addEquipment( new Equipment("Chair") );
on your previously initialized object of the Room-class, you will get
"Added new equipment: Chair"
Hope this helps a bit.
PS: The code is untestet (maybe there hides a syntax error somewhere)
Google endpoint message objects are very simple POJOs. I have a compound POJO that used to work but now is no longer working. The error I am getting, when android client makes the call, is that the JSON cannot be parsed because of AnimalTag. Here are the POJOs. To migrate to Java 7, I copied and pasted the code manually. So I thought that could have been the cause, that perhaps I left something out. But I can't think of what the problem may be. Other calls work fine. But this one keeps failing.
The usage is that the method receives Dog from client to save on server. Not all the data in Dog is filled, but many, including AnimalTag, are filled. Also, AnimalTag only has the manufacturer filled. Again This all used to work.
public class Dog {
private String name;
private String owner;
private AnimalTag tag;
public Dog(String name, String owner, AnimalTag tag) {
super();
this.name = name;
this.owner = owner;
this.tag = tag;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getOwner() {
return this.owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public AnimalTag getTag() {
return this.tag;
}
public void setTag(AnimalTag tag) {
this.tag = tag;
}
}
class AnimalTag{
private long number;
BlobKey imageKey;
String manufacturer;
public AnimalTag(long number, BlobKey imageKey, String manufacturer) {
super();
this.number = number;
this.imageKey = imageKey;
this.manufacturer = manufacturer;
}
public long getNumber() {
return this.number;
}
public void setNumber(long number) {
this.number = number;
}
public BlobKey getImageKey() {
return this.imageKey;
}
public void setImageKey(BlobKey imageKey) {
this.imageKey = imageKey;
}
public String getManufacturer() {
return this.manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
}
I got the answer, AnimalTag was missing the following constructor:
public AnimalTag(){}