Last problem to solve, null pointer exception with new ArrayList
I'm getting a NullPointerException when I run this program. The constructor points List<Meat> meats to new ArrayList(10) when the program is created. But When it tries to add objects to meats gets a nullpointerexception, why?
public Pantry(){
List<Garnish> garnishes=new ArrayList(10);
List<Sauce> sauces=new ArrayList(10);
List<Meat> meats=new ArrayList(10);
orderFood(Meat.class, 10);
orderFood(Garnish.class, 10);
orderFood(Sauce.class, 10);
}
publicvoid orderFood(Class foodType,int numItems){
for(int i= 0; i <= numItems; i++){
try{
addFood((Food) foodType.newInstance());}
catch( InstantiationException exception){
System.out.println(exception);}
catch(IllegalAccessException exception){
System.out.println(exception);
}
}
}
publicstaticvoid main(String[] args){
Pantry p =new Pantry();
p.displayContents();
System.out.println("What kind of Food would you like?");
String choice = KeyboardReader.readLine();
try{
Food order= p.getFood(Class.forName(choice));
if(order ==null){
System.out.println("Sorry, out of that type of food");
p.orderFood(Class.forName(choice), 10);
}
else{
System.out.println("Your order total is "+ order.getPriceToCharge());
}}
catch(ClassNotFoundException exception){
System.out.println(exception);
}
}
}

