0

I'm writing a program that manages inventory, like through adding, removing, and renting items, all by using multiple classes in an object-oriented atmosphere. Below is my implementation class (Driver).

Driver.java

public class Driver {
   public static void main(String[] args)
   {
      ABCRentals abc = new ABCRentals();
      Furniture item1 = new Furniture();
      boolean flag = abc.add(item1);
      List<Integer> idList = abc.getIDList();
      String[] itemTypes = abc.getItemArray();
      System.out.println("Item ID Numbers: " + Arrays.toString(idList.toArray()));
      System.out.println("Item Types: " + Arrays.toString(itemTypes));
   }
}

This code works just fine, as it is able to retrieve and print the ArrayList and String array through the getIDList() and getItemArray() methods, which are specified here in the ABCRentals class:

ABCRentals.java

public class ABCRentals implements Inventory
{
   private List<Integer> idNumbers = new ArrayList<Integer>();
   private List<Item> items = new ArrayList<Item>();
   private List<Integer> rentedItems = new ArrayList<Integer>();
   private String[] itemTypes2;

   public ABCRentals() { }

   public List<Integer> getIDList() {
      return idNumbers;
   }

   public List<Item> getItemList() {
      return items; 
   }

   public String[] getItemArray() {
      return itemTypes2;
   }

   public List<Integer> getRentedList() {
      return rentedItems;
   }

   public boolean add(Item item)
   {
      if (items.size() >= 300)
          return false;
      else
      {
         idNumbers.add(idNumbers.size() + 1);
         items.add(item);
         Object[] itemTypes = items.toArray();
         itemTypes2 = new String[itemTypes.length];
         int dvd = 0, f = 0, tv = 0;
         for (int index = 0; index < itemTypes.length; index++)
         {
            String indexValue = itemTypes[index].getClass().toString().substring(6);
            if (indexValue.equals("DVDPlayer"))
            {
              dvd++;
              itemTypes2[index] = indexValue + " #" + Integer.toString(dvd);
            }
            if (indexValue.equals("Furniture"))
            {
              f++;
              itemTypes2[index] = indexValue + " #" + Integer.toString(f);
            }
            if (indexValue.equals("Television"))
            {
              tv++;
              itemTypes2[index] = indexValue + " #" + Integer.toString(tv);
            }
          }
          return true;
      }
}

So when I try to access the ArrayList's and array's contents from the Driver class, it works. However, when I try to access the ArrayList or array from any other class, whether it's the DVDPlayer, Furniture, or Television class, I get a NullPointerException error, so in other words the ArrayList and array are not updated/populated with the values specified in the Driver class from the add() method and instead are returned as empty, unlike above. I'll use the Furniture class for example:

Furniture.java

public class Furniture implements Item
{
   private ABCRentals abc = new ABCRentals();
   public List<Integer> idList = new ArrayList<Integer>();
   public String[] itemArray = abc.getItemArray();
   private int ID;
   private static int idNumber = 0;

   public Furniture()
   {
      ID = ++idNumber;
   }

   public String getId()
   {
      int index = Arrays.asList(itemArray).indexOf("Furniture #" + Integer.toString(ID));
      id = Integer.toString(index);
      return id;
   }
}

And then adding this one line of code to the Driver class from above after the String[] itemTypes = abc.getItemArray() line results in the NullPointerException:

String id = item1.getId();

To make things simple, the Driver class can access the contents of the ArrayList and array stored in the ABCRentals class through the getIDList() and getItemArray() methods, but classes like Furniture cannot. The only difference seems to be one class has a main() method whereas the other doesn't. Am I not declaring ABCRentals objects in the Furniture class correctly to get updated values? IMPORTANT: I cannot change the getId() method to accept an argument like an ArrayList, because this is a homework assignment that requires to have the same classes and method signatures as specified. Sorry if this looks like a code dump, but I think it is important to understand every step I am trying to accomplish to understand my problem. Help would be much appreciated!

Max Voisard
  • 1,685
  • 1
  • 8
  • 18
  • You never seem to initialize `public String[] itemArray`, so it will be `null` when you call `Arrays.asList(itemArray)`. – Ivar Sep 21 '19 at 11:27
  • From the code you have shown I don't see you assign anything at all to `itemArray`. If you don't assign anything to it, it will remain `null` and therefore it will throw an NPE. You will need to assign a value _before_ you call `getId()`. (Or at least before the `Arrays.asList(itemArray)`.) – Ivar Sep 21 '19 at 11:34
  • @Ivar this may be hard to believe, but I accidentally mis-copied that line of code into this question. I edited my question to the line `public String[] itemArray = abc.getItemArray();` – Max Voisard Sep 21 '19 at 11:39
  • 1
    In that case `itemArray` still is `null`. You call `itemArray = abc.getItemArray()` where `abc` is a new instance. The `abc.add()` method is never called at the point you assign `getItemArray()` back to the `itemArray`. – Ivar Sep 21 '19 at 11:49

1 Answers1

2

You call your add() method in your main, which instantiates the itemTypes2 array. You don't call add() before accessing itemTypes2 in your Furniture class, so it is null.

Thomas Martin
  • 678
  • 8
  • 19