我开始同时处理三个课程,同时这样做我收到了上面的错误消息。我很困惑它为什么显示。我是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;
}
}
答案 0 :(得分:1)
您要declaring 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
。该实例的scope是构造函数体本身的本地。
更改
Item[] cart = new Item [MAXSIZE];
到
cart = new Item [MAXSIZE];
然后,
cart[itemCount] = new Item(itemName, price, quantity);
将不再抛出NullPointerException
,因为cart
的范围已扩展到整个班级。
答案 1 :(得分:0)
如果您尝试引用尚未声明/实例化的变量,则会获得NullPointer异常。例如,如果您声明如下: 列出新列表; 然后尝试做: newList.add(项目); 它会引发异常,因为新列表从未实例化过。如果它将它放在addToCart()函数中,很可能是因为您使用的其中一个变量尚未声明或实例化。当你到达那个函数调用时,我会调试并打印出每个变量的值,以确保它们具有与之关联的值。如果他们不是那可能只是你的问题。