Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9243945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:55:04+00:00 2026-06-18T08:55:04+00:00

I am writing two classes, one for Store, and one Item class for an

  • 0

I am writing two classes, one for Store, and one Item class for an assignment. I submit it online and it auto-grades according to an unknown tester.

I get the error: java.lang.NullPointerException

I assume it has something to do with returning null, however I was told to return null in one of the methods. If anyone can teach me what it is and how to fix it, thatd be great!

    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;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:55:05+00:00Added an answer on June 18, 2026 at 8:55 am

    This is your problem:

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

    The first time you call this, inventory is still null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm writing two anidated classes one is a dict the other a list? having
Assuming I had two classes, the first one for writing primitive types ( bool
I'm writing a simple script that replaces two images. The first one swaps in
I'm writing an application which has two classes that provide basically the same functionality
I have two classes now: EventsReader and EventsWriter . They actually work with one
I'm writing a program in rails where one class has the same behavior as
I've stumbled into one of two classic programming problems. I'm writing a Cocoa framework
I'm currently in a class on systems software development. We are writing the two-pass
I’m writing a client application with an accompanying GUI (Swing). My two classes, ClientClass
I'm new to linq and having trouble writing two simple queries. For some reason,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.