Bahaa Salaheldin
Bahaa Salaheldin

Reputation: 529

error: Cannot find getter for field in Android Room

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

Answers (4)

Val
Val

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

Boadu Philip Asare
Boadu Philip Asare

Reputation: 79

Change the variable from private to protected or public

from private int pCId;

to protected int pCId;

Upvotes: 5

nelion
nelion

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:

  1. create your variable
  2. right click the variable
  3. press "Alt + Insert" or press "Generate".
  4. Choose what you want autogenerated:

screenshot of how to autogenerate getter and/or setter

Upvotes: 0

madwyatt
madwyatt

Reputation: 362

This work for me:

  1. add the constructor with and without the auto generated id
  2. add the @Ignore annotation to the constructor without the auto generated id
  3. add the getter with camelCase
  4. 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

Related Questions