0

为什么我收到“线程中的异常mainjava.lang.NullPointerException?我在 PizzaDEMO 类的第 27 行收到此异常。该行显示为

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;               
        }       
}
4

5 回答 5

2

因为order[i]还在null。所以它试图执行null.addPizza(...).

在尝试执行数组元素的方法之前,您需要用一些东西填充数组。现在这一切null。类似的东西order[0] = new PizzaOrder();

于 2013-10-12T21:17:19.230 回答
0

有几个规则如何在运行时评估数组创建表达式。让我们注意这个具体步骤:

...创建一个具有指定长度的一维数组,并将数组的每个组件初始化为其默认值(第 4.12.5 节)。

但是,引用类型的默认值是多少?我知道你猜对了。来自java 语言规范的§4.12.5

...

对于所有引用类型(第 4.3 节),默认值为null

于 2013-10-12T21:41:47.457 回答
0

这里的问题是您在不指向对象的引用上调用该方法。
您必须为每个 order[i] 引用创建一个新的 PizzaOrder 对象。
在第一个 for 循环中,只需添加:

order[i] = new PizzaOrder();

:)

于 2013-10-12T21:23:17.783 回答
0

首先,您必须在 order 数组中输入一个 Pizzaorder 对象。如果没有订单,它就无法在其中添加比萨饼。披萨订单对象在 order[i] 中为空。

order[i]= new PizzaOrder(); 然后您可以获取 Order 对象并将比萨饼添加到此订单中

于 2013-10-12T21:24:01.727 回答
0

请记住,当您在 Java 中创建一个对象数组时,您会得到一个您希望用空值填充的大小的数组。尚无任何对象存在。如果您习惯于处理将获得默认值(数字基元为 0)的基元(int、boolean 等),这可能会令人困惑。

所以请记住这一点,当你点击这条线时:

order[i].addPizza(size, pepperoni, sasuage, mushrooms);

您是在告诉计算机“在顺序数组中的插槽 i 处找到我的对象”。不幸的是,因为那是空的,它给你一个空对象,然后你调用addPizza它!这就是你NullPointerException的来源。

解决方法是在遍历数组时创建每个对象。在循环顶部的某个地方,您需要执行以下操作:

order[i] = new PizzaOrder();

希望有帮助!

于 2013-10-12T21:24:15.317 回答