为什么我得到“线程中的异常”主“java.lang.NullPointerException”?

时间:2013-10-12 21:12:53

标签: java arrays nullpointerexception

为什么我收到“线程main中的异常java.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;               
        }       
}

5 个答案:

答案 0 :(得分:2)

因为order[i]仍然是null。所以它试图执行null.addPizza(...)

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

答案 1 :(得分:0)

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

order[i] = new PizzaOrder();

:)

答案 2 :(得分:0)

首先,你必须将一个吃零食品对象输入订单数组。没有订单,它不能添加比萨饼。披萨订单对象的顺序为[i]。

order [i] = new PizzaOrder(); 然后你可以使用Order对象,并可以将披萨添加到此订单中

答案 3 :(得分:0)

请记住,当您在Java中创建对象数组时,您将获得一个您想要填充空值的数组。目前还没有任何对象存在。如果您习惯于处理基元(int,boolean等),并且您将获得默认值(数字基元为0),这可能会令人困惑。

所以记住这一点,当你点击这一行时:

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

您告诉计算机“在订单数组中找到插槽i处的对象”。不幸的是,因为它是null,它会给你一个null对象,然后你就可以调用addPizza!这是NullPointerException来自的地方。

修复是在循环遍历数组时创建每个对象。在循环顶部的某处,你会想要做这样的事情:

order[i] = new PizzaOrder();

希望有所帮助!

答案 4 :(得分:0)

有几个rules如何在运行时计算数组创建表达式。让我们关注这一具体步骤:

  

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

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

  

...

     

对于所有参考类型(§4.3),默认值为 null