Having a problem creating objects and storing them in an arraylist. I have no problem doing this with regular arrays, but whenever I try to use an arraylist I get the error:
Exception in thread "main" java.lang.NullPointerException
at ZBay.printItems(main.java:41)
at main.processCommands(main.java:17)
at main.main(main.java:10)
import java.util.*;
public class main
{
public static void main(String[] parms)
{
ZBay store;
store = new ZBay();
processCommands(store);
System.out.println("\nProgram completed normally.");
}
public static void processCommands(ZBay store)
{
store.printItems();
}
}
class ZBay
{
ArrayList itemList;
public ZBay()
{
createItemList();
}
public void createItemList()
{
ArrayList itemList = new ArrayList();
itemList.add(new Item("test", "test", "test", "test"));
}
public void printItems()
{
Item currentItem;
currentItem = (Item) itemList.get(0);
System.out.println(currentItem.toString());
}
}
class Item
{
String itemId;
String itemName;
String itemPrice;
String vendorId;
String purchaserId;
public Item(String itemId, String itemName, String itemPrice, String vendorId)
{
this.itemId = itemId;
this.itemId = itemName;
this.itemPrice = itemPrice;
this.vendorId = vendorId;
}
public String toString()
{
return itemId + " " + itemName + " " + itemPrice + " " + vendorId;
}
}
Any help just pointing out where I am going wrong would be great!
Thanks.