为什么会出现“异常螺纹main
,java.lang.NullPointerException
我正在上PizzaDEMO类的第27行此不同的是行读取为什么我得到“线程异常”主“java.lang.NullPointerException”? ?
order[i].addPizza(size, pepperoni, sasuage, mushrooms);
输出是:
Enter the size s = small m = middom and l = large.
l
If it has pepperoni enter true, if not enter false.
true
If it has sasuage enter true, if not enter false.
true
If it has mushrooms enter true, if not enter false.
true
Exception in thread "main" java.lang.NullPointerException
at PizzaDEMO.main(PizzaDEMO.java:27)
Process completed.
谢谢: )
import java.util.*;
class PizzaDEMO
{
public static void main(String args [])
{
PizzaOrder[] order = new PizzaOrder[3];
for (int i = 0; i < order.length; i++)
{
Scanner scanner = new Scanner(System.in);
Scanner reader = new Scanner(System.in);
Scanner input = new Scanner(System.in);
char size = 'l';
boolean pepperoni = true;
boolean sasuage = true;
boolean mushrooms = true;
System.out.println("Enter the size s = small m = middom and l = large.");
size = reader.next().charAt(0);
System.out.println("If it has pepperoni enter true, if not enter false.");
pepperoni = scanner.nextBoolean();
System.out.println("If it has sasuage enter true, if not enter false.");
sasuage = scanner.nextBoolean();
System.out.println("If it has mushrooms enter true, if not enter false.");
mushrooms = scanner.nextBoolean();
order[i].addPizza(size, pepperoni, sasuage, mushrooms);
}
for (int i = 0; i == order.length; i++)
{
System.out.println(order[i].cost());
}
}
}
/*
class PizzaDEMO
{
public static void main(String args [])
{
PizzaOrder order = new PizzaOrder();
order.addPizza('l', true, true, true);
order.addPizza('s', true, true, true);
order.addPizza('s', true, false, true);
order.addPizza('m', false, false, false);
order.addPizza('m', false, true, false);
System.out.println(order.cost());
}
}
*/
class PizzaOrder
{
private final int MAXPIZZAS = 10;
private Pizza[] pizzas = new Pizza[MAXPIZZAS];
int numPizzas = 0;
public void addPizza(char size, boolean pepperoni, boolean sasuage, boolean mushrooms)
{
if(numPizzas!=MAXPIZZAS)
{
pizzas[numPizzas] = new Pizza(size, pepperoni, sasuage, mushrooms);
numPizzas++;
}
}
public double cost()
{
double total = 0;
for (int i=0; i<numPizzas;i++)
{
if(pizzas[i].getSize()=='s')
total += 8;
else if(pizzas[i].getSize()=='m')
total += 10;
else if(pizzas[i].getSize()=='l')
total += 12;
else
System.out.println("Not a Size");
total+= pizzas[i].getNumToppings();
}
return total;
}
}
/* Chapter No. 12 - Exercise No. 2 [REQUIRED: otherwise zero points]
File Name: Pizza.java
Programmer: Jared Wines
Date Last Modified: Oct. 12, 2013
Problem Statement: This program with record the cost of the pizza with the size and toppings of the pizza.
Overall Plan (step-by-step, how you want the code to make it happen):
1. Make a pizza class with all the constutors and intance varible.
2. Make a pizza order class that caluates the cost of the pizza.
3. Make a pizzmdemo that inputs the pizza data than outputs the cost of the pizza.
Classes needed and Purpose (Input, Processing, Output)
main class – MyProgram
*/
public class Pizza
{
private char size;
private boolean sasuage;
private boolean pepperoni;
private boolean mushrooms;
public Pizza(char size, boolean pepperoni, boolean sasuage, boolean mushrooms)
{
this.size=size;
this.pepperoni=pepperoni;
this.sasuage=sasuage;
this.mushrooms=mushrooms;
}
public char getSize()
{
return size;
}
public int getNumToppings()
{
int count = 0;
if(pepperoni)
count++;
if(sasuage)
count++;
if(mushrooms)
count++ ;
return count;
}
}
*为什么我得到“线程中的异常”main“java.lang.NullPointerException”?*因为您在应用程序中使用了具有'null'值的变量。您可以通过阅读错误的堆栈跟踪来查看**哪里**。 –
你没有在'order'数组中实例化任何'PizzaOrder'元素。 –