title: Shopping Cart Java Application (addToCart)
body: <p>I am doing a Java Application for a class and need help with errors that are coming up after running the app. </p>
  
  <p>The app is supposed to allow the user to add an item to the shopping cart(item name, price, quantity). The user described item will be added to the shopping cart array, and then it will be printed out once it is successfully added. </p>
  
  <p>The shopping cart has a max capacity of 5 items initially, but if the cart size is > 5, it will be increased + 3, via the increaseSize method. </p>
  
  <p>As stated below, I believe that the addToCart method is not adding the user described item to the cart. That is why it is a null value within the toString method. </p>
  
  <p>So, I basically need help with the addToCart method. 
  Please correct me if I am wrong. Thank you. </p>
  
  <p>An output from running the app is as follows:</p>
  
  <pre><code>run:
  Enter the name of the item: a
  Enter the unit price: 2
  Enter the quantity: 2
  Exception in thread "main" java.lang.NullPointerException
      at shop.ShoppingCart.toString(ShoppingCart.java:62)
      at java.lang.String.valueOf(String.java:2854)
      at java.io.PrintStream.println(PrintStream.java:821)
      at shop.Shop.main(Shop.java:49)
  Java Result: 1
  </code></pre>
  
  <p>Below is the ShoppingCart Class.</p>
  
  <p>The toString method should not have any errors being produced, but I believe that the problem lies within the addToCart method. </p>
  
  <pre><code>// **********************************************************************
  //   ShoppingCart.java
  //
  //   Represents a shopping cart as an array of items
  // **********************************************************************
  import java.text.NumberFormat;
  
  public class ShoppingCart
  {
  
      private Item[] cart;
      private int itemCount;      // total number of items in the cart
      private double totalPrice;  // total price of items in the cart
      private int capacity;       // current cart capacity
  
      // -----------------------------------------------------------
      //  Creates an empty shopping cart with a capacity of 5 items.
      // -----------------------------------------------------------
      public ShoppingCart()
      {
  
        capacity = 5;
        cart = new Item[capacity];
        itemCount = 0;
        totalPrice = 0.0;
      }
  
      // -------------------------------------------------------
      //  Adds an item to the shopping cart.
      // -------------------------------------------------------
      public void addToCart(String itemName, double price, int quantity)
      { 
  
          Item temp = new Item(itemName, price, quantity);
          totalPrice += (price * quantity);
          itemCount += quantity;
          cart[itemCount] = temp;
          if(itemCount==capacity)
          {
              increaseSize();
          }
      }
  
      // -------------------------------------------------------
      //  Returns the contents of the cart together with
      //  summary information.
      // -------------------------------------------------------
      public String toString()
      {
        NumberFormat fmt = NumberFormat.getCurrencyInstance();
  
        String contents = "\nShopping Cart\n";
        contents += "\nItem\t\tUnit Price\tQuantity\tTotal\n";
  
        for (int i = 0; i &lt; itemCount; i++)
            contents += cart[i].toString() + "\n";
  
        contents += "\nTotal Price: " + fmt.format(totalPrice);
        contents += "\n";
  
        return contents;
      }
  
      // ---------------------------------------------------------
      //  Increases the capacity of the shopping cart by 3
      // ---------------------------------------------------------
      private void increaseSize()
      {
          Item[] temp = new Item[capacity+3];
          for(int i=0; i &lt; capacity; i++)
          {
              temp[i] = cart[i];
          }
          cart = temp; 
          temp = null;
          capacity = cart.length;
      }
  }
  </code></pre>
  
  <p>Below is the Shop Main. </p>
  
  <p>The ArrayList is not in use at this time, but will be later once the toString errors are corrected. </p>
  
  <pre><code>package shop;
  
  // ***************************************************************
  //   Shop.java
  //
  //   Uses the Item class to create items and add them to a shopping
  //   cart stored in an ArrayList.
  // ***************************************************************
  
  import java.util.ArrayList;
  import java.util.Scanner;
  
  public class Shop
  {
      public static void main (String[] args)
      {
        ArrayList&lt;Item&gt; cart = new ArrayList&lt;Item&gt;();
  
        Item item;
        String itemName;
        double itemPrice;
        int quantity;
  
        Scanner scan = new Scanner(System.in);
  
        String keepShopping = "y";
        ShoppingCart cart1 = new ShoppingCart();
        do
            {
              System.out.print ("Enter the name of the item: ");
              itemName = scan.next();
  
              System.out.print ("Enter the unit price: ");
              itemPrice = scan.nextDouble();
  
              System.out.print ("Enter the quantity: ");
              quantity = scan.nextInt();
  
              // *** create a new item and add it to the cart
              cart1.addToCart(itemName, itemPrice, quantity);
  
  
  
              // *** print the contents of the cart object using println
              System.out.println(cart1);
  
              System.out.print ("Continue shopping (y/n)? ");
              keepShopping = scan.next();
            }
        while (keepShopping.equals("y"));
  
      }
  }
  </code></pre>
  
  <p>Below is the Item Class:</p>
  
  <pre><code>package shop;
  
  // ***************************************************************
  //   Item.java
  //
  //   Represents an item in a shopping cart.
  // ***************************************************************
  
  import java.text.NumberFormat;
  
  public class Item
  {
      private String name;
      private double price;
      private int quantity;
  
      // -------------------------------------------------------
      //  Create a new item with the given attributes.
      // -------------------------------------------------------
      public Item (String itemName, double itemPrice, int numPurchased)
      {
        name = itemName;
        price = itemPrice;
        quantity = numPurchased;
      }
  
      // -------------------------------------------------------
      //   Return a string with the information about the item
      // -------------------------------------------------------
      public String toString ()
      {
        NumberFormat fmt = NumberFormat.getCurrencyInstance();
  
        return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t"
              + fmt.format(price*quantity));
      }
  
      // -------------------------------------------------
      //   Returns the unit price of the item
      // -------------------------------------------------
      public double getPrice()
      {
        return price;
      }
  
      // -------------------------------------------------
      //   Returns the name of the item
      // -------------------------------------------------
      public String getName()
      {
        return name;
      }
  
      // -------------------------------------------------
      //   Returns the quantity of the item
      // -------------------------------------------------
      public int getQuantity()
      {
        return quantity;
      }
  } 
  </code></pre>