narko
narko

Reputation: 3875

Execution failed for task ':app:compileDebugJavaWithJavac'. > java.util.NoSuchElementException: List is empty

I started using Room in a new project and started getting this problem after creating my database and some DAOs. Now gradle is not even able to compile the project. See image below:

enter image description here

I am pretty sure the problem is due to a mistake I did with Room. I am attaching the code of my data model below.

Basically, I have a database with 2 entities: RecipeEntry and IngredientEntry. I have tried to model the relation in the way you see below. I also use a POJO called RecipeWithIngredients in order to encapsulate the data that is returned in the query modelled in RecipeDao. The reason for choosing this approach is that Room forbids you to model relationships in the same way other ORMs do.

Because SQLite is a relational database, you can specify relationships between objects. Even though most ORM libraries allow entity objects to reference each other, Room explicitly forbids this.

More info about this here.

@Entity(tableName = "recipe")
public class RecipeEntry {
   @PrimaryKey
   private int id;
   private String name;
   private String image;
   private int servings;
}

@Entity(tableName = "ingredient")
public class IngredientEntry {
   @PrimaryKey(autoGenerate = true)
   private int id;
   private int quantity;
   private String measure;
   private String description;
   @ColumnInfo(name = "recipe_id")
   private int recipeId;
}

public class RecipeWithIngredients {
   @Embedded
   private RecipeEntry recipe;
   @Relation(parentColumn = "id", entityColumn = "recipe_id", entity =  IngredientEntry.class)
   private List ingredients;

   public RecipeEntry getRecipe() {
      return recipe;
   }

   public void setRecipe(RecipeEntry recipe) {
      this.recipe = recipe;
   }

   public List getIngredients() {
      return ingredients;
   }

   public void setIngredients(List ingredients) {
      this.ingredients = ingredients;
   }
}

@Dao
public interface RecipeDao {
   @Query("SELECT * FROM recipe, ingredient WHERE recipe.id = :recipeId AND ingredient.recipe_id = recipe.id")
   List<RecipeWithIngredients> getRecipeWithIngredients(int recipeId);
}

Any clue about what's going on?

Upvotes: 2

Views: 961

Answers (1)

narko
narko

Reputation: 3875

After fighting with this issue for a couple of hours I found out that the problem is in the RecipeWithIngredients POJO. You need to specify the kind of objects that you are returning in the List, otherwise Room will have issues. It would be cool if the Room developers could print any kind of message in this respect. The current error description is not too helpful. The modified code is as follows:

public class RecipeWithIngredients {
   @Embedded
   private RecipeEntry recipe;
   @Relation(parentColumn = "id", entityColumn = "recipe_id", entity = IngredientEntry.class)
   private List<IngredientEntry> ingredients;

   public RecipeEntry getRecipe() {
      return recipe;
   }

   public void setRecipe(RecipeEntry recipe) {
      this.recipe = recipe;
   }

   public List<IngredientEntry> getIngredients() {
      return ingredients;
   }

   public void setIngredients(List<IngredientEntry> ingredients) {
      this.ingredients = ingredients;
   }
}

Upvotes: 1

Related Questions