我正在编写两个类,一个用于Store,一个用于类的Item。我在线提交,并根据未知的测试人员自动评分。
我收到错误:java.lang.NullPointerException
我认为它与返回null有关,但我被告知在其中一个方法中返回null。如果有人能教我这是什么以及如何解决它,那就太好了!
import java.util.ArrayList;
import java.util.Scanner;
public class Store {
private ArrayList<Item> inventory;
// CONSTRUCTORS
/*
* Constructs a store without any items in its inventory.
*/
public Store() {
}
/*
* Constructs a store by reading items from a given Scanner. The constructor
* must repeatedly (until item name is *) read items from the given scanner
* object and add it to its inventory. Here is an example of the data (that
* has three items) that could be entered for reading from the supplied
* scanner:
*/
public Store(Scanner keyboard) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s1 = sc.nextLine();
if (s1.equals("*")) {
break;
} else {
Scanner ls = new Scanner(s1);
while (ls.hasNext()) {
Item item = new Item(ls.next(), ls.nextInt(), ls.nextInt());
inventory.add(item);
}
}
}
}
// MOTHODS
/*
* Finds an item by its name if it is part of the store's inventory Name is
* case-insensitive Returns the Item object corresponding to the given name
* if the item was found. If an item with the given name was not found, then
* this method returns null.
*/
public Item findItem(String name) {
for (Item item : inventory) {
if (item.getName().equalsIgnoreCase(name)) {
return item;
}
}
return null;
}
/*
* Updates existing item or adds a new item to the inventory. If an item
* with the same name as the given item already exists in the inventory,
* then this method updates the quantity for the given item.
*/
public void add(Item item) {
for (Item items : inventory) {
if (items.getName().equalsIgnoreCase(item.getName())) {
items = item;
} else {
inventory.add(item);
}
}
}
/*
* Performs operations reflecting selling an item from the store's
* inventory. If the given item is not found in the inventory then this
* method prints a message and returns null. If sufficient quantity of item
* is not available then this method reports an error and returns null.
* Otherwise (if the item is found and sufficient quantity is present in the
* inventory) then this method removes the requested quantity from the
* inventory and returns a new item that contains information about the item
* purchased.
*/
public Item sellItem(String name, int quantity) {
for (Item items : inventory) {
if (items.getName().equalsIgnoreCase(name)) {
if (items.getQuantity() >= quantity) { // if name is equal and
// quantity is enough
@SuppressWarnings("unused")
Item ret = new Item(name, items.getUnitPrice(), quantity);
items.changeQuantity(-1 * (quantity));
} else {// if name is there, but not enough quantity
System.out.println("Error: Found, but not enough quantity");
return null;
}
} else {
System.out.println("Error: The item was not found.");
return null;
}
}
return null;
}
/*
* Performs operations reflecting return of an item back to the store's
* inventory. An item can only be returned to inventory if such an item
* previously existed in the inventory. So, if you try to add bread to the
* inventory, but there was never bread in the inventory in the first place,
* then this method will not put the bread back on the shelf. If the given
* item is not found in the inventory then this method prints a message and
* returns false indicating the return was not accepted. Otherwise (if the
* item is found) this method adds the returned quantity to the appropriate
* item entry in its inventory and returns true.
*/
public boolean returnItemToInventory(String name, int quantity) {
for (Item items : inventory) {
if (items.getName().equalsIgnoreCase(name)) { // if name exists
items.changeQuantity(quantity); // adds quantity
return true;
} else { // it didnt exist
System.out.println("ERROR: Never existed.");
return false;
}
}
return false;
}
/*
* Returns a String representation of this store, consisting of a list of
* all the store's inventory, and the net value (in dollars) of the store's
* inventory. Formatting will be as shown in this example:
*/
public String toString() {
String ret = "";
int re = 0;
for (Item items : inventory) {
ret += items.toString();
re += items.getTotalPrice();
}
ret += "Net inventory price: " + re;
return ret;
}
}
答案 0 :(得分:3)
您从未初始化inventory
。
public Store() {
inventory = new ArrayList<Item>();
}
可能还有其他可能发生NPE的地方。请下次告诉我们您的来源中发生异常的确切行。
答案 1 :(得分:2)
运行代码后我得到了这个
Exception in thread "main" java.lang.NullPointerException
at swing7.Store.<init>(Store.java:36)
at swing7.Store.main(Store.java:145)
表明inventory
显然从未实例化过:
private ArrayList<Item> inventory;
轻松修复:
inventory = new ArrayList<Item>();
答案 2 :(得分:1)
这是你的问题:
public void add(Item item) {
for (Item items : inventory) {
if (items.getName().equalsIgnoreCase(item.getName())) {
items = item;
} else {
inventory.add(item);
}
}
}
第一次调用时,inventory
仍然为空。