Reputation: 529
This is a simple a class in android studio:
package com.loghty.bahaa.loghty;
import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import android.support.annotation.NonNull;
@Entity (tableName = "Chars")
public class Chars {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "char_Id")
private int pCId;
@NonNull
@ColumnInfo(name = "char_field")
private String mcharField;
// Getters and setters
public String getMcharField() {
return mcharField;
}
public void setMcharField(@NonNull String mcharField) {
this.mcharField = mcharField;
}
public int getpCId() {
return pCId;
}
}
But when I build the app I get this error in the pCId field:
error: Cannot find getter for field
I checked the code many times but nothing is strange. where is the error exactly ?
Upvotes: 5
Views: 10395
Reputation: 4366
I had the same issue when the getter fun return type was different from the field's type (DateTime
vs String
):
val timestamp: String = DateTime.now().toString()
fun getTimestamp(): DateTime = DateTime.parse(timestamp)
Room was skipping a generation of getTimestamp()
fun for the timestamp
field because Room was thinking it's already created. Wheres, the type mismatch caused an exception because types are not matching.
The solution was to rename the getter fun:
val timestamp: String = DateTime.now().toString()
fun getTime(): DateTime = DateTime.parse(timestamp)
Upvotes: 1
Reputation: 79
Change the variable from private to protected or public
from private int pCId;
to
protected int pCId;
Upvotes: 5
Reputation: 1962
if you are using Room and the variable is a boolean, then remember that the getter is named isVariableName() and not getVariableName().
I would highly suggest that - if the error should come again - then use Android Studio to autogenerate your getter and setter. That way you won't run into this issue again.
To do so:
Upvotes: 0
Reputation: 362
This work for me:
clean all the data before the first install on device because i don't want to do the migration.
@Entity(tableName = "words")
public class Word {
@PrimaryKey(autoGenerate = true)
private int id;
@NonNull
@ColumnInfo(name = "word")
private String mWord;
public Word(int id, @NonNull String mWord) {
this.id = id;
this.mWord = mWord;
}
@Ignore
public Word(@NonNull String mWord) {
this.mWord = mWord;
}
public int getId() { return id; }
public String getWord(){ return this.mWord; }
}
Upvotes: 2