I am trying to add a database to my Android app through the Room Persistence library. At compilation a get the above error. In addition Room can't find my getters, although i can clearly see all of them in my code.
Here's my code for the entity:
@Entity(tableName = "users", indices = @Index(value = "username", unique = true))
public class User {
@NonNull
public String getuId() {
return uId;
}
public void setuId(@NonNull String uId) {
this.uId = uId;
}
public String getuUsername() {
return uUsername;
}
public void setuUsername(String uUsername) {
this.uUsername = uUsername;
}
@NonNull
public String getuPassword() {
return uPassword;
}
public void setuPassword(@NonNull String uPassword) {
this.uPassword = uPassword;
}
public String getuEmail() {
return uEmail;
}
public void setuEmail(String uEmail) {
this.uEmail = uEmail;
}
@NonNull
@PrimaryKey
@ColumnInfo(name = "user_id")
private String uId;
@ColumnInfo(name = "username")
private String uUsername;
@NonNull
@ColumnInfo(name = "password")
private String uPassword;
@ColumnInfo(name = "email")
private String uEmail;
@Ignore
public User(String userName, String email, String password){
uId = UUID.randomUUID().toString();
uUsername = userName;
uEmail = email;
uPassword = password;
}
public User(@NonNull String id, String username, String email,@NonNull String password){
this.uId = id;
this.uUsername = username;
this.uPassword = password;
this.uEmail = email;
}
}
And the error i get:
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: User(java.lang.String,java.lang.String,java.lang.String,java.lang.String) : [id : null, username : null, email : null, password : null]
Also this:
Error:(53, 20) error: Cannot find getter for field. Error:(56, 20) error: Cannot find getter for field. Error:(60, 20) error: Cannot find getter for field. Error:(63, 20) error: Cannot find getter for field.
For my first question, as suggested by CommonsWare and rmlan, was to change the parameter names in my constructor to match those of the variables.
The problem with the getters was that Room uses the JavaBeans Conventions for their names, so for example my getuUsername()
should have rather been getUUsername()
. After I changed all of the getters to match that the build was successful.
Source: Android Developers
try this:-
@Ignore
public User(int uId, String uUsername, String uEmail, String
uPassword){
this.uId = uId;
this.uUsername = uUsername;
this.uEmail = uEmail;
this.uPassword = uPassword;
}
public User(String uUsername, String uEmail, String
uPassword){
this.uUsername = uUsername;
this.uEmail = uEmail;
this.uPassword = uPassword;
}
It simply means that you must have unannotated parameterless constructor like so
@Ignore
public User(int uId, String uUsername, String uEmail, String
uPassword){
this.uId = uId;
this.uUsername = uUsername;
this.uEmail = uEmail;
this.uPassword = uPassword;
}
//parameterless constructor
//un annotated
public User(){
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With