1
votes

I am new in android architecture component. I am trying to build app with the reference of android-architecture-components/BasicRxJavaSample with the same Gradle configuration. But I get the following errors..

  • Error:(14, 8) error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).Tried the following constructors but they failed to match:= Department(java.lang.String,java.lang.String) : [id : null, dep : null]
  • Error:(16, 20) error: Cannot find setter for field.
  • Error:(18, 20)error: Cannot find setter for field.

I am using Version "1.0.0-alpha9" for all architecture Components. I have tried with current "1.0.0-beta1" version also.

I have looked [Ambiguous getter for Field… Room persistence library 2 link also but according to google sample not need to setter for every field.

Model class

    @Entity(tableName = "tblDepartment")
public class Department{
    @PrimaryKey
    private String mId;

    private String mName;

    @Ignore
    public Department(String dep) {
        mId= UUID.randomUUID().toString();
        mName= dep;
    }
    public Department(String id, String dep) {
        mId = id;
        mName=dep;
    }

    public String getDepartmentId() {
        return mId;
    }

    public String getDepartmentName() {
        return mName;
    }
}
3
Your problem is with an Expense class. Your question shows a Department class. Either you have the wrong error message or the wrong code. - CommonsWare
I mistakenly added wrong error log. - Biswanath Maity

3 Answers

4
votes

Your problem is that your getters/setters don't match your field names so currently you have:

private String mId;

public String getId() {}

The Room preprocessor is looking through your class and failing to find a getter/setter for your mId field because it is expecting public String getMId() Same with your constructor, the parameter names are not matching the field names so it can not find a valid constructor.

0
votes

You need a valid constructor trough the names.

And getters/setters for all fields you are storing trough Room.

Try:

@Entity(tableName = "tblDepartment")
public class Department{
@PrimaryKey
private String mId;

private String mName;

@Ignore
public Department(String mName) {
    mId= UUID.randomUUID().toString();
    this.mName= mName;
}
public Department(String id, String name) {
    this.mId = mId;
    this.mName=mName;
}

public String getId() {
    return mId;
}

public String getName() {
    return mName;
}
public void setId(String mId){ this.mId = mId; }
public void setName(String mName){ this.mName = mName; }
} 
0
votes

you can also try this:-

@Ignore
public Department(int mId, String mName) {
   this.mId= mId;
   this.mName= mName;
}

public Department(String mName) {
   this.mName= mName;
}