2013-10-12 43 views
0

为什么会出现“异常螺纹mainjava.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;    
     }  
} 
+4

*为什么我得到“线程中的异常”main“java.lang.NullPointerException”?*因为您在应用程序中使用了具有'null'值的变量。您可以通过阅读错误的堆栈跟踪来查看**哪里**。 –

+2

你没有在'order'数组中实例化任何'PizzaOrder'元素。 –

回答

2

因为order[i]仍然null。所以它试图给exec ute null.addPizza(...)

在试图执行数组元素的方法之前,您需要用某些东西填充数组。现在它全部是null。类似于order[0] = new PizzaOrder();

0

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

order[i] = new PizzaOrder(); 

:)

0

首先,你必须输入一个pizzaorder对象为顺序排列。没有订单它不能添加比萨饼。匹配顺序对象在[i]的顺序中为空。

order [i] = new PizzaOrder(); 然后你可以拿订单对象,并可以添加披萨到这个顺序

0

记住,当你在Java中创建一个对象数组,你会得到一个你想用空值填充大小的数组。没有任何对象存在。如果你习惯于处理原始数据(int,boolean等),你会得到一个默认值(对于数字原语为0),这可能会让人困惑。

所以牢记这一点,当你打这一行:

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

你告诉电脑“我找对象,在槽i的顺序排列的。”不幸的是,因为它是空的,它给你一个空对象,然后你打电话给它addPizza!这是您的NullPointerException来自哪里。

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

order[i] = new PizzaOrder(); 

希望帮助!

0

有几个rules如何在运行时评估数组创建表达式。让我们注意这个特定的步骤:

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

但是,引用类型的默认值是什么?我知道你猜对了。从Java语言规范的§4.12.5

...

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

相关问题