16

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.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
v4570
  • 490
  • 1
  • 3
  • 14

4 Answers4

14

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

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
v4570
  • 490
  • 1
  • 3
  • 14
1

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;
}
Nathani Software
  • 111
  • 1
  • 1
  • 7
1

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

}
  • still not working after adding an empty public constructor – Vivek Pratap Singh May 02 '20 at 10:18
  • @VivekPratapSingh After building the project, click on the empty constructor of your table class, it will open out your Dao impl class, open it. Usually it will point out errors which will be the root cause of your problem. – anp8850 May 20 '20 at 18:55
0

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.

CoolMind
  • 26,736
  • 15
  • 188
  • 224