java.lang.NullPointerException

Hi there. I've tried to remove the java.lang.NullPointerException error for hours but to no avail. Would anyone please point out what causes the problem in my code? Here goes...


import java.io.*;
import java.util.*;

public class MyShop {
  private String shopName = "ITGrocers";       //name of the shop
  int capacity = 2;                            //maximum number of items that the shop can sell
  Item[] listOfItems;    //list of items in the shop    
  int numItems = 0;                            //number of items in the shop
  
  public String getShopName() {
    return shopName;
  }
  
  public void setShopName(String name) {
    shopName = name;
  }
  
  public int addItem(String itemName, double itemPrice) {
    boolean itemInShop = false; 
    if (numItems != 0) {
      for(int i = 0; i < numItems; i++) {
        if (itemName.equalsIgnoreCase(listOfItems[i].getItemName()))
          itemInShop = true;
      }
    }
    if (itemInShop)
      return 0;
    
    if (numItems == capacity)
      return 1;
    
    else {
      listOfItems[numItems].setItemName(itemName);
      listOfItems[numItems].setItemPrice(itemPrice);
      numItems += 1;
      return 2;
    }
  }
  
  public boolean updateItemPrice(String itemName, double newItemPrice) {
    boolean itemInShop = false;
    for(int i = 0; i < numItems; i++) {
      if (listOfItems[i].getItemName().equalsIgnoreCase(itemName)) {
        listOfItems[i].setItemName(itemName);
        listOfItems[i].setItemPrice(newItemPrice);
        itemInShop = true;
      }
    }
    if (itemInShop)
      return true; 
    else
      return false;
  }
  
  public void display() {
    System.out.println("Shop name: " + shopName + "\nMaximum capacity: " + capacity);
    if (numItems == 0)
      System.out.println("No item added yet!\n");
    else {
      System.out.println("Total: " + numItems + " item(s)");
      for (int i = 0; i < numItems; i++) 
      System.out.println(listOfItems[i].toString() + "\n");
    }
  }
  
  public static void main (String[] args) {
    MyShop obj = new MyShop();
    Item[] listOfItems = new Item[obj.capacity];
    for (int i = 0; i < obj.capacity; i++) {
      listOfItems[i] = new Item();
    }
    boolean loopChoice = true;
    int choice;
    double itemPrice;
    String itemName;
    Scanner s = new Scanner(System.in);
    while (loopChoice) {
      loopChoice = false;
      System.out.println("--------------------Welcome to Shop Management Program--------------------");
      System.out.println("1.Add Item\n2.Update Item Price\n3.Display all items\n4.Exit");
      System.out.println("Enter choice:");
      choice = s.nextInt();
      s.nextLine();
      
      switch (choice) {
        case 1: {
          System.out.print("Enter item name:");
          itemName = s.nextLine();
          System.out.print("Enter item price:");
          itemPrice = s.nextDouble();
          if (obj.addItem(itemName,itemPrice) == 0)
            System.out.println("This item is already in the list.");
          else if (obj.addItem(itemName,itemPrice) == 1)
            System.out.println("Adding failed. The shop has already reached its maximum capacity.");
          else
            System.out.print("New item added successfully.");
          loopChoice = true;
          break;
        }
        
        case 2: {
          System.out.println("Enter item name:");
          itemName = s.nextLine();
          System.out.println("Enter item new price:");
          itemPrice = s.nextDouble();
          if (obj.updateItemPrice(itemName,itemPrice))
            System.out.println("Item updated successfully.");
          else
            System.out.println("The item does not exist.");
          loopChoice = true;
          break;
        }
        
        case 3: {
          obj.display();
          loopChoice = true;
          break;
        }
         
        case 4: {
          System.out.print("Thank you for using the program. Goodbye!");
          break;
        }
        
        default: {
          System.out.println("Invalid choice!");
          loopChoice = true;
        }
      }
    }
  }
} 


class Item {
  private String itemName;
  private double itemPrice;
  
  public String getItemName() {
    return itemName;
  }
  public void setItemName(String n) {
    itemName = n;
  }
  
  public double getItemPrice() {
    return itemPrice;
  }
  public void setItemPrice(double p) {
    itemPrice = p;
  }
  
  public String toString() {
    return itemName + "\t$" + itemPrice;
  }
}

The error lies at line 36 and 94 which are as follows:


listOfItems[numItems].setItemName(itemName);

and


if (obj.addItem(itemName,itemPrice) == 0)

Explanations would be helpful. Thanks in advance:)

Join the discussion
Be the first to comment on this article. Our Commenting Policies