[英]Why is my application throwing a NullPointerException?
我開始同時處理三個類,同時我收到了上面的錯誤消息。 我對它為什么顯示感到非常困惑。 我是 Java 新手,所以它可能是我看不到的非常簡單的東西。 我在 addToCart 方法下的購物車類上收到錯誤。 我知道這是很多東西要看,但我真的很感激我能得到的任何幫助。
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;
}
}
在這里你聲明了cart
數組
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
。 該實例的范圍是構造函數體本身的局部范圍。
改變
Item[] cart = new Item [MAXSIZE];
到
cart = new Item [MAXSIZE];
然后,
cart[itemCount] = new Item(itemName, price, quantity);
將不再拋出NullPointerException
,因為cart
的范圍已擴展到整個類。
如果您嘗試引用尚未聲明/實例化的變量,則會出現 NullPointer 異常。 例如,如果您聲明如下: List newList; 然后嘗試做: newList.add(item); 它會拋出異常,因為從未實例化新列表。 如果它在 addToCart() 函數中拋出它,很可能是因為您正在使用的變量之一尚未聲明或實例化。 當您到達該函數調用時,我會調試並打印出每個變量的值,以確保它們具有與其關聯的值。 如果他們不這樣做,那可能只是你的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.