I am trying to add a database to my android app through the Room Persistence library and i am getting this error:
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: User(int,java.lang.String,java.lang.String,int,int,int,java.lang.String) -> [param:id -> matched field:unmatched, param:name -> matched field:unmatched, param:gender -> matched field:unmatched, param:age -> matched field:unmatched, param:weight -> matched field:unmatched, param:height -> matched field:unmatched, param:workout -> matched field:unmatched]
Here is my code:
@Entity
public class User {
@PrimaryKey
private int userId;
private String userName;
private String userGender;
private int userAge;
private int userWeight;
private int userHeight;
private String workoutPlan;
public User(int id, String name, String gender, int age, int weight, int height, String workout) {
this.userId = id;
this.userName = name;
this.userGender = gender;
this.userAge = age;
this.userWeight = weight;
this.userHeight = height;
this.workoutPlan = workout;
} ...
Can someone please tell me what i am doing wrong or what i missed?
I also got this error. Only one of my attributes in my constructor did not match and I was finding it rather confusing. My variable (outside the constructor) was named mURL and I was trying to match it to url within the constructor.
Instead, I should have set the outer variable to mUrl. Yes, capitalising the last 2 letters gave me that error. Setting
this.mUrl = url
instead ofthis.mURL = url
solved the error.In my case, I solved problem just removing
@Ignore
from default constructor.If you copied your code from some place, and have
@Ignore
in default constructor then need to remove@Ignore
from default constructor.Before:
After:
I just added because it's happen with me.
you can also try this:-
I got around this same problem by just adding a empty constructor.
public User(){}
Please change names of the parameters such that it matches entity attributes.
For persistance, it uses JavaBeans conventions in Room. For more information: https://developer.android.com/training/data-storage/room/defining-data#java