v4570
v4570

Reputation: 490

Room Persistence: Entities and POJOs must have a usable constructor

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.

Upvotes: 16

Views: 16129

Answers (4)

CoolMind
CoolMind

Reputation: 28793

See also Room Persistence: Error:Entities and Pojos must have a usable public constructor.

In my case I had to change class like:

@Entity(tableName = "country")
data class CountryEntity(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    var id: Int = 0,
    @ColumnInfo(name = "country_id")
    var countryId: Int? = null,
    @ColumnInfo(name = "language")
    var language: String? = null,
    @ColumnInfo(name = "country_name")
    var countryName: String? = null,
)

Note that I use var, @PrimaryKey and initialize all fields with values. You can use constructor() : this(0, null, null, null) instead.

Upvotes: 0

v4570
v4570

Reputation: 490

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

Upvotes: 14

Dominic mundia
Dominic mundia

Reputation: 67

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(){

}

Upvotes: 1

Nathani Software
Nathani Software

Reputation: 111

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;
}

Upvotes: 1

Related Questions