11

I'm having this error when trying to compile the project:

Error:Execution failed for task ':app:compileDebugJavaWithJavac'. java.lang.IllegalArgumentException: intcannot be converted to an Element

And this warning:

Warning:Supported source version 'RELEASE_7' from annotation processor 'android.arch.persistence.room.RoomProcessor' less than -source '1.8'

These are my database related classes:

@Entity(tableName = "users")
public class SerializedUser {

    @PrimaryKey(autoGenerate = true)
    private int id;

    @ColumnInfo(name = "first_name")
    private String firstName;

    @ColumnInfo(name = "last_name")
    private String lastName;

    @ColumnInfo(name = "username")
    private String username;

    public SerializedUser(int id, String firstName, String lastName, String username) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.username = username;
    }

    public int getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getUsername() {
        return username;
    }
}

@android.arch.persistence.room.Database(entities = {SerializedUser.class}, version = 4)
public abstract class Database extends RoomDatabase {
    public abstract UserDao userDao();

    private static final String DATABASE_NAME = "weather";

    // For Singleton instantiation
    private static final Object LOCK = new Object();
    private static volatile Database i;

    public static Database init(Context context) {
        if (i == null) {
            synchronized (LOCK) {
                if (i == null) {
                    i = Room.databaseBuilder(context.getApplicationContext(),
                            Database.class, Database.DATABASE_NAME)
                            .fallbackToDestructiveMigration().build();
                }
            }
        }
        return i;
    }

    public static boolean isInited(){
        return i != null;
    }

    public static Database getInstance(){
        if(i == null)
            throw new NullPointerException("Database.getInstance called when Database not initialized");
        return i;
    }
}

@Dao
public abstract class UserDao {

    Converters converters;

    public void inject(Converters converters) {
        this.converters = converters;
    }

    @Insert(onConflict = REPLACE)
    public abstract void saveNow(SerializedUser user);

    @Delete
    public abstract void deleteNow(int id);

    @Query("DELETE FROM users")
    public abstract void deleteAllNow();

    @Query("SELECT * FROM users")
    public abstract List<SerializedUser> getAllNow();

    @Query("SELECT * FROM users ORDER BY last_name ASC")
    public abstract LivePagedListProvider<Integer, SerializedUser> usersByLastName();
}

So the error happens when it tries to implement the database classes? That id on SerializedUser is not the problem, I have commented it out, the problem was still the same. Tried to clean and rebuild the project and restarted Android Studio (invalidate and restart).

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Scarass
  • 914
  • 1
  • 12
  • 32

3 Answers3

13

remove

@Delete
public abstract void deleteNow(int id);

from your Dao it will work

houman.sanati
  • 1,054
  • 3
  • 18
  • 34
Vikash
  • 179
  • 1
  • 7
  • 2
    A little explanation would be good, gonna try if this solve my issue or not but I still don't understand why. Would be nice if you include why I should remove this. – Ali_Nass Nov 25 '18 at 18:40
  • 3
    you can delete an Entity not an id you can write query to delete user by ID . Please refer https://stackoverflow.com/questions/47538857/android-room-delete-with-parameters?rq=1 – Vikash Dec 04 '18 at 07:13
  • 1
    To use the `@Delete` annotation you need to provide a Room Entity. You can use `@Query` if you want to delete an Entity using the `id` like: `@Query("DELETE FROM clothes WHERE id = :Id") public abstract void deleteById(int id)` – ravi Sep 16 '19 at 11:02
  • The same applies to `@Insert` annotated methods: the argument must be an entity. – Imaxd Jan 26 '20 at 17:14
9

@Delete annotation marks a method in a Dao annotated class as a delete method. The implementation of the method will delete its parameters from the database.
All of the parameters of the Delete method must either be classes annotated with Entity or collections/array of it.

Read here for extra information.

So in your case you pass a parameter with int type which violates the aforementioned rule. That is why you are getting that error.

In order to resolve this issue, you should either exclude deleteNow method or just pass any parameter that does not violates the rule that was mentioned above.

2

Error:Execution failed for task ':app:compileDebugJavaWithJavac'. java.lang.IllegalArgumentException: intcannot be converted to an Element

Basically, this problem arises not only by the @Delete query, but by all the Room's CRUD annotations (@Insert, @Delete, @Update) except @Query.

All of the parameters of these CRUD annotated methods must either be classes annotated with Entity or collections/array of it.

So we can't pass primitive or other than these.

Lee Mac
  • 15,615
  • 6
  • 32
  • 80