Reputation:
I get this error but I couldn't find any solution. I tried many things but still I'm getting this code.
My code is: https://justpaste.it/4di3y
The error:
error: Cannot find getter for field. ( private Long fie_id )
How I can fix it?
Upvotes: 13
Views: 22449
Reputation: 302
Kotlin room does not allow defining variables in entity class start of 'i' character. I had the same error. I solved it, albeit difficult. Replace id with pId and it will be fine. For example;
val isrc: String? = "",
instead of
val albumIsrc: String? = "",
If you change the variable in this way, the error will be resolved. Happy codding.
Note : if system language of your computers is Turkish, this case does.
Upvotes: 6
Reputation: 2729
Instead of
public Long getId() {
return fie_id;
}
public void setId(Long id) {
this.fie_id = id;
}
Do
public Long getFie_id() {
return fie_id;
}
public void setFie_id(Long fie_id) {
this.fie_id = fie_id;
}
Upvotes: 10
Reputation: 2179
Maybe you don't believe me but i have to say. I am using Mac and that problem was about the Computer Language. I was using Turkish Language and i have changed to English and that problem was gone after Invalidate Cache and Restart.
As summary:
Upvotes: 6
Reputation: 947
If everything is correct, then I have observe this issue mostly in a case of variable kind of pId, uId etc.
change variable name from
private Long pId;
to
private Long pid;
And generate getter and setter using IDE.
Upvotes: 0
Reputation: 79
Changing the variable from private to protected worked for me.
Upvotes: 1
Reputation: 301
Try to match the name of the setter function as the variable name declared.
Forex.
There is a boolean variable declared like
private boolean attachment_status;
It's getter should be like this
public boolean getattachment_status(){
return attachment_status
}
or u can do like this
public boolean getAttachment_status(){
return attachment_status
}
I declared my getter like this and I was getting the same error
public boolean getAttachment(){
return attachment_status
}
but I refactor it and the problem was solved.
Upvotes: 2
Reputation: 347
For me it was because of an uncapitalized "m". The below works:
@ColumnInfo(name = "foo")
private String mFoo;
public String getMFoo() {
return mFoo;
}
public void setMFoo(String foo) {
this.mFoo = foo;
}
This doesn't:
@ColumnInfo(name = "foo")
private String mFoo;
public String getmFoo() {
return mFoo;
}
public void setmFoo(String foo) {
this.mFoo = foo;
}
Also don't forget to specify the new column in your migration. Something like:
val MIGRATION_2_3: Migration = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE myDataObj "
+ " ADD COLUMN foo TEXT")
}
}
Upvotes: 3
Reputation: 399
Change all the access modifier to "public". This will solve the issue.
Instead of:
@PrimaryKey
private Long fie_id;
@ColumnInfo(name = "name")
private String name;
Use:
@PrimaryKey
public Long fie_id;
@ColumnInfo(name = "name")
public String name;
Upvotes: 5
Reputation: 1633
Only need to change is fied scope update as "public" instead of "private".
https://github.com/rzwitserloot/lombok/issues/1403
Upvotes: 0
Reputation: 11
for getter in setter, just follow the Pattern get/set[A-Z]last lenght-1 of the field just like in the example ( field: mWord)
@Entity(tableName = "words")
public class Word {
@PrimaryKey
@NonNull
@ColumnInfo(name = "word")
private String mWord;
public Word(@NonNull String mWord) {
this.mWord = mWord;
}
public String getMWord() {
return mWord;
}
}
Upvotes: 0
Reputation: 6617
It is better to stick with documantation. The structures are sensitive. link of documantation Sample :
@Entity(tableName = "notes")
public class Note {
@PrimaryKey(autoGenerate = true)
public int id;
@NotNull
@ColumnInfo(name = "note")
public String mNote;
public Note(@NotNull String mNote) {
this.mNote = mNote;
}
public int getId() {
return id;
}
@NotNull
public String getNote() {
return this.mNote;
}}
Had same problem Clear
and Rebuild
the project options are the answer somehow after the change.
Upvotes: 0
Reputation: 600
During your programs lifecycle if the encapsulation is incorrect then confusion happens. After viewing your code it seems might be attempting to access something from an eternal class. PrimaryKey id is being called instead of the passed value setId( id ).
Upvotes: 1
Reputation: 2953
It has to do with the naming of your PrimaryKey
. The signature of your getter and setter should correspond to the name of the variable. Otherwise, Room
can't find it.
Which means that the variable id
should have getId()
, and variable fie_id
should have getFie_id()
as a getter, the same goes for the setters. So, either rename your PrimaryKey
or name your getters and setters accordingly.
Upvotes: 4