2013-10-12 61 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節),默認值是

相關問題