2014-04-03 60 views
-1

我開始一次處理三個類,同時這樣做我收到了上面的錯誤消息。我很困惑它爲什麼顯示。我是新來的Java,所以它可能是非常簡單的東西,我只是看不到。我正在使用addToCart方法獲取購物車類中的錯誤。我知道這是很多東西要看,但我會很感激任何和所有幫助,我可以得到。爲什麼我的應用程序拋出一個NullPointerException?

public class ShoppingCart 
{ 
    private int itemCount;  //total number of items in the cart 
    private double totalPrice;  //total price of items in the cart 
    private final int MAXSIZE = 100; // cart maximum capacity 
    private Item[]cart; 

    //creates an empty shopping cart 
    public ShoppingCart() 
    { 
     Item[]cart = new Item [MAXSIZE]; 
     itemCount = 0; 
     totalPrice = 0.0; 
    } 

    //adds an item to the shopping cart 
    public void addToCart(String itemName, double price, int quantity) 
    { 

     cart[itemCount] = new Item(itemName, price, quantity); 
     totalPrice = (totalPrice + (quantity * price)); 
     itemCount++; 

    } 
    //returns the contents on the cart together with summary information 
    public String toString() 
    { 
     String contents = "\nShopping Cart\n"; 
     contents = contents + String.format("%-12s%-12s%-10s%-7s%n", "Item", 
     "Unit Price", "Quantity", "Item Total"); 

     for(int i = 0; i<itemCount; i++) 
     contents = contents + cart[i].toString() + "\n"; 

     contents = contents + String.format("%20s$ %.2fn","CurrentTotal:", totalPrice); 

     return contents; 
    } 

} 













import java.util.*; 

public class Shop 
    { 
    public static void main (String[] args) 
    { 
     ShoppingCart myCart = new ShoppingCart(); 
     Scanner kbd = new Scanner(System.in); 




     String itemName; 
     double itemPrice; 
     int quantity; 

     String keepShopping = "y"; 

     do 
     { 
      System.out.print ("Enter the name of the item: "); 
      itemName = kbd.nextLine(); 

      System.out.print ("Enter the unit price: "); 
      itemPrice = kbd.nextDouble(); 

      System.out.print ("Enter the quantity: "); 
      quantity = kbd.nextInt(); 

      myCart.addToCart(itemName, itemPrice, quantity); 

      System.out.print ("Continue shopping (y/n)? "); 
      keepShopping = kbd.next(); 
      kbd.nextLine(); 
     } 
     while (keepShopping.equals("y")); 

     System.out.println("Have a Nice Day!"); 

    } 
} 










import java.text.NumberFormat; 

public class Item 
{ 
    private String name; 
    private double unitPrice; 
    private int quantity; 

    // ------------------------------------------------------- 
    // Create a new item with the given attributes. 
    // ------------------------------------------------------- 
    public Item (String itemName, double itemPrice, int numPurchased) 
    { 
     name = itemName; 
     unitPrice = itemPrice; 
     quantity = numPurchased; 
    } 

    // ------------------------------------------------------- 
    // Return a string with the information about the item 
    // ------------------------------------------------------- 

    public String toString() 
    { 
     return String.format("%-15s$%-8.2f%-11d$%-8.2f", name, unitPrice, quantity,  unitPrice*quantity); 
    } 

    // ------------------------------------------------- 
    // Returns the unit price of the item 
    // ------------------------------------------------- 
    public double getPrice() 
    { 
     return unitPrice; 
    } 

    // ------------------------------------------------- 
    // Returns the name of the item 
    // ------------------------------------------------- 
    public String getName() 
    { 
     return name; 
    } 

    // ------------------------------------------------- 
    // Returns the quantity of the item 
    // ------------------------------------------------- 
    public int getQuantity() 
    { 
     return quantity; 
    } 
} 
+0

因爲當我做它說它沒有正確格式。我做了什麼,沒有工作,所以我不得不張貼圖片 – user3494953

+0

如果有很多需要看的話,請爲我們做這項工作:取消你的改變,直到你沒有得到錯誤,然後在一個班級工作,直到再次出現錯誤爲止:添加最後一件事導致了錯誤,您應該從那裏開始調試 –

+0

您至少可以發佈堆棧跟蹤。無需格式化。 – Veluria

回答

1

給您declaringcart陣列

private Item[]cart; 

這個構造函數中,要初始化它

public ShoppingCart() { 
    Item[]cart = new Item [MAXSIZE]; 
    itemCount = 0; 
    totalPrice = 0.0; 
} 

,但是當您嘗試訪問這裏的要素之一(cart[itemCount]),它拋出一個NullPointerException

public void addToCart(String itemName, double price, int quantity) { 
    cart[itemCount] = new Item(itemName, price, quantity); 
    totalPrice = (totalPrice + (quantity * price)); 
    itemCount++; 
} 

這是因爲,雖然您正在正確聲明該數組,但只要構造函數結束,它就會立即返回到null。該實例的scope對構造函數體本身是本地的。

變化

Item[] cart = new Item [MAXSIZE]; 

cart = new Item [MAXSIZE]; 

然後,

cart[itemCount] = new Item(itemName, price, quantity); 

將不再拋出NullPointerException,因爲cart的範圍已經擴大到整個班級。

+0

非常感謝。我現在無法檢查,因爲我在工作,但是。我相信你知道你在說什麼。謝謝大家回覆 – user3494953

+0

不客氣。祝你好運。 – aliteralmind

0

如果您試圖引用未聲明/實例化的變量,您將得到NullPointer異常。例如,如果你聲明如下: List newList; 然後試着做: newList.add(item); 它會引發異常,因爲新列表從未實例化。如果將它引入addToCart()函數,很可能是因爲您使用的變量之一未被聲明或實例化。當你進入該函數調用時,我會調試並打印出每個變量的值,以確保它們具有與它們相關的值。如果他們不這樣做可能只是你的問題。

相關問題