user8143839
user8143839

Reputation:

Android Room library error: Cannot find getter for field

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

Answers (14)

EL HIRCH BADREDDYN
EL HIRCH BADREDDYN

Reputation: 171

Make the variable public: Private --> Public

Upvotes: 1

fevziomurtekin
fevziomurtekin

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

TomH
TomH

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

oguzhan
oguzhan

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:

  • Set MacOSX language as English (SystemPreferences -> Language & Region)
  • Open Android Studio and Perform Invalidate Caches And Restart (File -> Invalidate Caches/Restart -> Invalidate and Restart)

Upvotes: 6

kunal khedkar
kunal khedkar

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

Boadu Philip Asare
Boadu Philip Asare

Reputation: 79

Changing the variable from private to protected worked for me.

Upvotes: 1

Abhishek Meharia
Abhishek Meharia

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

Blaze Gawlik
Blaze Gawlik

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

Robiul Hossain Shah
Robiul Hossain Shah

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

Sathish Gadde
Sathish Gadde

Reputation: 1633

Only need to change is fied scope update as "public" instead of "private".

https://github.com/rzwitserloot/lombok/issues/1403

Upvotes: 0

abderazak la3ta3ta
abderazak la3ta3ta

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

Samir
Samir

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

EvOlaNdLuPiZ
EvOlaNdLuPiZ

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

Suleyman
Suleyman

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

Related Questions