#Exception in thread main- Cannot invoke java.util.Collection.toArray() because c is null

4 messages · Page 1 of 1 (latest)

modern lava
#

I am getting this compilation error in the sectionArrayList<Recipe> tempRecipes = new ArrayList<Recipe>(recipesKnown); as well as the end of my program below(this is part of a bigger file that I cut out most of for simplicity):

    private Collection<Recipe> recipesKnown;
    private Collection<Ingredient> itemsOwned;
    private Collection<Store> nearbyStores;

    public MealPlanner(Collection<Recipe> recipesKnown, Collection<Ingredient> itemsOwned, Collection<Store> nearbyStores) {
        recipesKnown = new ArrayList<Recipe>();
        itemsOwned = new HashSet<Ingredient>();
        nearbyStores = new ArrayList<Store>();
    }
    
    private Collection<Recipe> createTempRecipes() {
        ArrayList<Recipe> tempRecipes = new ArrayList<Recipe>(recipesKnown); 
        for(Ingredient ing : itemsOwned) {
            double ownedAmount = ing.getAmount();
            for(Recipe recipe : tempRecipes) {
                ArrayList<Ingredient> neededIngredients = new ArrayList<Ingredient>(recipe.getIngredients()); //get the ingredients for each recipe
                for(Ingredient ingredient : neededIngredients) {
                    double neededAmount = ingredient.getAmount();
                    if(neededIngredients.contains(ing) && neededAmount <= ownedAmount) { 
                        
neededIngredients.remove(ing); 
                    }
                    else {
                        neededAmount -= ing.getAmount(); 
                    }
                }
                
            }
        }
        return tempRecipes;
    }
    ArrayList<Recipe> tempRecipes = new ArrayList<Recipe>(createTempRecipes());```
Besides this, everything else works fine. Any help would be appreciated!
tidal sableBOT
#

Hey, @modern lava!
Please remember to /close this post once your question has been answered!

#
I am getting this compilation error in the sectionArrayList<Recipe> tempRecipes = new ArrayList<Recipe>(recipesKnown); as well as the end of my program below(this is part of a bigger file that I cut out most of for simplicity): 
public class MealPlanner {
    private Collection<Recipe> recipesKnown;
    private Collection<Ingredient> itemsOwned;
    private Collection<Store> nearbyStores;

    public MealPlanner(Collection<Recipe> recipesKnown, Collection<Ingredient> itemsOwned, Collection<Store> nearbyStores) {
        recipesKnown = new ArrayList<Recipe>();
        itemsOwned = new HashSet<Ingredient>();
        nearbyStores = new ArrayList<Store>();
    }
    
    private Collection<Recipe> createTempRecipes() {
        ArrayList<Recipe> tempRecipes = new ArrayList<Recipe>(recipesKnown); 
        for(Ingredient ing : itemsOwned) {
            double ownedAmount = ing.getAmount();
            for(Recipe recipe : tempRecipes) {
                ArrayList<Ingredient> neededIngredients = new ArrayList<Ingredient>(recipe.getIngredients()); //get the ingredients for each recipe
                for(Ingredient ingredient : neededIngredients) {
                    double neededAmount = ingredient.getAmount();
                    if(neededIngredients.contains(ing) && neededAmount <= ownedAmount) { 
                        
neededIngredients.remove(ing); 
                    }
                    else {
                        neededAmount -= ing.getAmount(); 
                    }
                }
                
            }
        }
        return tempRecipes;
    }
    ArrayList<Recipe> tempRecipes = new ArrayList<Recipe>(createTempRecipes());
Besides this, everything else works fine. Any help would be appreciated!