0

私は 2 つのクラスを作成しています。1 つは Store 用、もう 1 つは割り当て用の Item クラスです。オンラインで提出すると、未知のテスターに​​従って自動採点されます。

次のエラーが表示されます: java.lang.NullPointerException

nullを返すことと関係があると思いますが、メソッドの1つで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;
    }

}
4

3 に答える 3

3

を初期化していませんinventory

public Store() {
    inventory = new ArrayList<Item>();
}

NPE が発生する可能性のある場所が他にもある可能性があります。次回は、ソース内で例外が発生した正確な行を教えてください。

于 2013-02-05T20:11:44.593 に答える
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>();
于 2013-02-05T20:12:39.613 に答える
1

これはあなたの問題です:

public void add(Item item) {
    for (Item items : inventory) {
        if (items.getName().equalsIgnoreCase(item.getName())) {
            items = item;
        } else {
            inventory.add(item);
        }
    }
}

これを初めて呼び出すときinventoryは、まだ null です。

于 2013-02-05T20:14:31.307 に答える