Scarass
Scarass

Reputation: 944

Room IllegalArgumentException: intcannot be converted to an Element

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).

Upvotes: 11

Views: 5742

Answers (3)

Vikash
Vikash

Reputation: 179

remove

@Delete
public abstract void deleteNow(int id);

from your Dao it will work

Upvotes: 13

vivek singh
vivek singh

Reputation: 21

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.

Upvotes: 2

Anastasios Vlasopoulos
Anastasios Vlasopoulos

Reputation: 1802

@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.

Upvotes: 9

Related Questions