Question: I am getting a null pointer exception as seen below, code to follow: run: Please enter the recipe name: cake Please enter the number of
I am getting a null pointer exception as seen below, code to follow:
run: Please enter the recipe name: cake Please enter the number of servings: 10 Please enter an ingredient name using letters only. Enter 'e' if you are finished sugar Please enter the unit of measure for the ingredient (ex.- cup, oz, tsp, tbsp, pt). Use letters only:) Please enter the unit of measure for the ingredient (ex.- cup, oz, tsp, tbsp, pt). Use letters only:) cup How many cups of sugar?. Enter numbers only! 5 Please enter the number of calories per cup. Use numbers only! 500 Please enter an ingredient name using letters only. Enter 'e' if you are finished flour Please enter the unit of measure for the ingredient (ex.- cup, oz, tsp, tbsp, pt). Use letters only:) Please enter the unit of measure for the ingredient (ex.- cup, oz, tsp, tbsp, pt). Use letters only:) cup How many cups of flour?. Enter numbers only! 5 Please enter the number of calories per cup. Use numbers only! 500 Please enter an ingredient name using letters only. Enter 'e' if you are finished e Recipe: null Serves: 0.0 Exception in thread "main" java.lang.NullPointerException at recipebox.RecipeBox.printRecipe(RecipeBox.java:319) at recipebox.RecipeBox.main(RecipeBox.java:464) Java Result: 1 BUILD SUCCESSFUL (total time: 24 seconds)
package recipebox;
import java.util.Scanner; import java.util.ArrayList;
/** * * */ class Ingredient { private double servings; private String ingredientName; private double ingredientAmount; private double ingredientCalories; private String unitMeasurement; private double ingredientAmountPerServing = ingredientAmount / servings;
/** * @param ingredientName * @param ingredientAmount * @param unitMeasurement * @param ingredientCalories // * @param total recipe calories */ public Ingredient(String ingredientName, double ingredientAmount, String unitMeasurement, double ingredientAmountPerServing, double ingredientCalories) { this.ingredientName = ingredientName; this.ingredientAmount = ingredientAmount; this.ingredientCalories = ingredientCalories; this.unitMeasurement = unitMeasurement; this.ingredientAmountPerServing = ingredientAmountPerServing; } /** * * @return name of ingredient */ public String getIngredientName() { return ingredientName; }
/** * * @param ingredientName to set */ public void setIngredientName(String ingredientName) { this.ingredientName = ingredientName; }
/** * * @return amount of ingredient */ public double getIngredientAmount() { return ingredientAmount; }
/** * * @param ingredientAmount to set */ public void setIngredientAmount(double ingredientAmount) { this.ingredientAmount = ingredientAmount; }
/** * * @return calories in ingredient per unit measurement */ public double getIngredientCalories() { return ingredientCalories; }
/** * * @param ingredientCalories to set */ public void setIngredientCalories(double ingredientCalories) { this.ingredientCalories = ingredientCalories; }
public String getUnitMeasurement() { return unitMeasurement; }
public void setUnitMeasurement(String unitMeasurement) { this.unitMeasurement = unitMeasurement; }
public double getServings() { return servings; }
public void setServings(double servings) { this.servings = servings; }
public double getIngredientAmountPerServing() { return ingredientAmountPerServing; }
public void setIngredientAmountPerServing(double ingredientAmountPerServing) { this.ingredientAmountPerServing = ingredientAmountPerServing; }
}
/** * * @author davidamonahan */ class RecipeBox {
private String recipeName;
private double totalRecipeCalories;
private static ArrayList
private static ArrayList
private double servings;
private double ingredientAmount;
private static double ingredientAmountPerServing;
private String unitMeasurement;
/** * * @param recipeName * @param totalRecipeCalories * @param recipeIngredients * @param servings */ public RecipeBox(String recipeName, double totalRecipeCalories, ArrayList
/** * returns the list of created recipes by name * @return * */ public static ArrayList
private RecipeBox(String recipeName, double servings, ArrayList
/** * uses the list of recipes created in Ultimate Recipe Builder * @param listOfRecipes */ public void setListOfRecipes(ArrayList
RecipeBox.listOfRecipes = listOfRecipes; } public double getIngredientAmountPerServing() { return ingredientAmountPerServing; }
/** * @param ingredientAmountPerServing the ingredientAmountPerServing to set */ public void setIngredientAmountPerServing(double ingredientAmountPerServing) { this.ingredientAmountPerServing = ingredientAmountPerServing; }
/** gets the unit of measurement for ingredient (cup, oz, tsp, etc.) * @return the unitMeasurement */ public String getUnitMeasurement() { return unitMeasurement; }
/** sets the unit of measurement for the ingredient * @param unitMeasurement the unitMeasurement to set */ public void setUnitMeasurement(String unitMeasurement) { this.unitMeasurement = unitMeasurement; } /** * returns name of recipe * * @return */ public String getRecipeName() {
return recipeName; }
/** * name of recipe to set * @param recipeName */ public void setRecipeName(String recipeName) {
this.recipeName = recipeName; }
/** * * @return total calories per recipe */ public double getTotalRecipeCalories() {
return totalRecipeCalories; }
/** * total calories per recipe to set * @param totalRecipeCalories */ public void setTotalRecipeCalories(double totalRecipeCalories) {
this.totalRecipeCalories = totalRecipeCalories; }
/** * * @return list of ingredients for recipe */ public static ArrayList
return recipeIngredients; }
/** * sets list of ingredients for recipe * @param recipeIngredients */ public void setRecipeIngredients(ArrayList
this.recipeIngredients = recipeIngredients; }
/** * * @return # servings for recipe */ public double getServings() {
return servings; }
/** * # of servings to set * @param servings */ public void setServings(double servings) {
this.servings = servings; }
public double getIngredientAmount() {
return ingredientAmount; }
/** * name of recipe to set * @param recipeName */ public void setIngredientAmount(double ingredientAmount) {
this.ingredientAmount = ingredientAmount; } /** * prints list of recipes */ public void printAllRecipeNames() { listOfRecipes.stream().forEach((recipe) -> { System.out.println(recipe.getRecipeName()); }); }
/** * prints recipe name, servings, ingredients, single serving calories */ public void printRecipe() { // print method to display recipe double getIngredientAmountPerServing = (getIngredientAmount() / getServings());
double singleServingCalories = (getTotalRecipeCalories() / getServings());
String unitMeasurement = "";
System.out.println("Recipe: " + getRecipeName());
System.out.println("Serves: " + getServings());
getRecipeIngredients().stream().forEach((ingredient) -> {
System.out.println(ingredient.getIngredientName() + ", " + ingredient.getIngredientAmount() + ", "
+ ingredient.getIngredientCalories());
});
System.out.println("Each serving has " + singleServingCalories + " Calories."); } public static RecipeBox createNewRecipe(){
Scanner scnr = new Scanner(System.in);
ArrayList
double ingredientCalories = 0; // # calories per each ingredient double ingredientAmount = 0; // amount of ingredient defined by unit of measure (cup, tsp, etc.) double servings = 0; // # servings per recipe boolean addMoreIngredients = true; double totalRecipeCalories = ingredientCalories * ingredientAmount; // total calories from ingredients System.out.println("Please enter the recipe name: "); String recipeName = scnr.nextLine(); // gets name of recipe
while (!recipeName.matches("[a-zA-Z_]+")) { // validates user input is letters, repeats statement until letters entered System.out.println("Invalid name, use letters only");
recipeName = scnr.nextLine(); }
System.out.println("Please enter the number of servings: "); // gets # servings
while (!scnr.hasNextDouble()) { // verifies input is pos # System.out.println("Please enter the number of servings" + ". Use numbers only "); // validates user inputs numbers for servings, repeats until numbers entered scnr.nextLine();
} servings = scnr.nextDouble();
scnr.nextLine();
do{ System.out.println("Please enter an ingredient name using letters only. Enter 'e' if you are finished"); // gets first ingredient String ingredientName = scnr.next(); if (ingredientName.toLowerCase().equals("e")) { // breaks loop of adding more ingredients, goes to print recipe addMoreIngredients = false; // allow to add more ingredients } else{ while (!ingredientName.matches(".*[a-z].*")) { System.out.println("Please enter an ingredient name. Use letters only"); ingredientName = scnr.next(); // validates user input is letters, repeats until letters entered } System.out.println("Please enter the unit of measure for the ingredient " + "(ex.- cup, oz, tsp, tbsp, pt). Use letters only:)"); // gets unit of measurement for ingredient String unitMeasurement = scnr.nextLine(); while (!unitMeasurement.matches(".*[a-z].*")) { // validates user input is letters and repeats until letters entered System.out.println("Please enter the unit of measure for the ingredient " + "(ex.- cup, oz, tsp, tbsp, pt). Use letters only:)"); unitMeasurement = scnr.nextLine(); } System.out.println("How many " + unitMeasurement + "s of " + ingredientName + "?. Enter numbers only!"); // gets ingredient amount while (!scnr.hasNextDouble()) { System.out.println("How many " + unitMeasurement + "s of " + ingredientName + "?. Enter numbers only!"); // validates user input is numbers for amount, repeats until numbers entered scnr.nextLine(); } ingredientAmount = scnr.nextDouble(); System.out.println("Please enter the number of calories per " + unitMeasurement + ". Use numbers only!"); // gets # calories per amount of ingredient while (!scnr.hasNextDouble()) { System.out.println("Please enter the number of calories per " + unitMeasurement + ". Use numbers only!"); // validates user input # calories is numbers, repeats until number entered scnr.nextLine(); } ingredientCalories = scnr.nextDouble(); scnr.nextLine(); Ingredient ingredient = new Ingredient(ingredientName, ingredientAmount, unitMeasurement, ingredientAmountPerServing, ingredientCalories); recipeIngredients.add(ingredient); totalRecipeCalories += (ingredientCalories * ingredientAmount); }
} while(addMoreIngredients);
RecipeBox recipe = new RecipeBox(recipeName, servings, recipeIngredients, ingredientAmountPerServing, totalRecipeCalories); // creates recipe- name, # servings, ingredients, ingredient amount per serving, // total calorie recipes listOfRecipes.add(recipe);
return recipe; // prints recipe } /** * menu options to create recipe, display list of recipes, delete recipe, * adjust ingredient amount per serving, or print details of selected recipe * @param args * */ public static void main(String[] args) { RecipeBox newRecipe = createNewRecipe(); listOfRecipes.add(newRecipe); newRecipe.printRecipe(); Scanner menuScnr = new Scanner(System.in);
System.out.println("Please select a menu item: " + "1. Add Recipe " + "2. Print Recipe " + "3. Print Recipe Names " + "4. Adjust Recipe Servings " + "5. Delete Recipe "); while (menuScnr.hasNextInt() || menuScnr.hasNextLine()) { int input = menuScnr.nextInt();
if (input == 1) {
newRecipe = createNewRecipe(); listOfRecipes.add(newRecipe); newRecipe.printRecipe(); System.out.println("Please select a menu item: " + "1. Add Recipe " + "2. Print Recipe " + "3. Print Recipe Names " + "4. Adjust Recipe Servings " + "5. Delete Recipe ");
} else if (input == 2) {
System.out.println("Which recipe? ");
String selectedRecipeName = menuScnr.next(); boolean found = true; for (RecipeBox recipeBox : listOfRecipes) { if (!recipeBox.getRecipeName().equals(selectedRecipeName)) { found = false; System.out.println("This recipe does not exist!"); } if (found) { recipeBox.printRecipe(); } }
System.out.println("Please select a menu item: " + "1. Add Recipe " + "2. Print Recipe " + "3. Print Recipe Names " + "4. Adjust Recipe Servings " + "5. Delete Recipe ");
} else if (input == 3) { for (int j = 0; j < listOfRecipes.size(); j++) { System.out.println((j + 1) + ": " + listOfRecipes.get(j).getRecipeName()); System.out.println("Please select a menu item: " + "1. Add Recipe " + "2. Print Recipe " + "3. Print Recipe Names " + "4. Adjust Recipe Servings " + "5. Delete Recipe "); } } else if (input == 4){ System.out.println("Which recipe? ");
String selectedRecipeName = menuScnr.next(); RecipeBox recipe = null;
for (RecipeBox recipeBox : listOfRecipes) { if (recipeBox.getRecipeName().equals(selectedRecipeName)) { recipe = recipeBox; } } if (recipe != null) { System.out.println ("How many servings would you like? Enter numbers only!"); while (!menuScnr.hasNextDouble()) { System.out.println("How many servings would you like? Enter numbers only!"); menuScnr.nextLine();
}
double servings = menuScnr.nextDouble(); recipe.setServings(servings); System.out.println("New recipe yield); " + servings); getRecipeIngredients().stream().forEach((ingredient) -> { System.out.println(ingredient.getIngredientName() + "," + ingredient.getIngredientAmountPerServing() + ingredient.getUnitMeasurement() + "per serving, " + "has " + ingredient.getIngredientCalories()); }); System.out.println("Please select a menu item: " + "1. Add Recipe " + "2. Print Recipe " + "3. Print Recipe Names " + "4. Adjust Recipe Servings " + "5. Delete Recipe "); } else if (input == 5){ System.out.println("Which recipe? "); selectedRecipeName = menuScnr.next();
for (RecipeBox recipeBox : listOfRecipes) { if (recipeBox.getRecipeName().equals(selectedRecipeName)) { recipe = recipeBox; } } if (recipe != null) { listOfRecipes.remove(recipe); System.out.println("New Recipe List: "); for (int j = 0; j < listOfRecipes.size(); j++) { System.out.println((j + 1) + ": " + listOfRecipes.get(j).getRecipeName()); } } System.out.println("Please select a menu item: " + "1. Add Recipe " + "2. Print Recipe " + "3. Print Recipe Names " + "4. Adjust Recipe Servings " + "5. Delete Recipe "); } } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
