Help me find the null pointer pointee

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
I swear I'm going blind I can't see it, I get the following exception when trying to run the program

Exception in thread "main" java.lang.NullPointerException
at paul.vmachine.domain.Slot.refillSlot(Slot.java:44)
at paul.vmachine.domain.VendMachine.initSlot(VendMachine.java:45)
at paul.vmachine.gui.VendGuiAWT.<init>(VendGuiAWT.java:98)
at paul.vmachine.gui.VendGuiAWT.main(VendGuiAWT.java:294)

So tracing it back we have

A new gui frame thing created in main()
Code:
   public static void main(String [] args) {
      VendGuiAWT gui = new VendGuiAWT();
      gui.launchFrame();
   }

The VendGuidAWT() constructor creating a VendMachine object and initialising the slot with a new Item
Code:
vMachine = new VendMachine();
vMachine.initSlot("0","Mars",0.4);
...

The VendMachine() constructor which creates an ArrayList of Slot objects
Code:
   public VendMachine() {
      this.slotList = new ArrayList<Slot>(MAX_SLOTS);
      this.currentBalance = 0.0;
      for(int i=0;i<MAX_SLOTS;i++) {
      	this.slotList.add(i,new Slot(Integer.toString(i)));
      }
   }

The Slot() constructor
Code:
   public Slot(String slotID) {
      this.slotID = slotID;
   }

The iniSlot() object which gets a Slot object and refills it with a new Item
Code:
   public void initSlot(String slotID,String itemName,double itemCost) {
      this.slotList.get(Integer.parseInt(slotID)).refillSlot(new Item(itemName,itemCost));
   }

The Item() constructor
Code:
   public Item(String name,double cost) {
      this.name = name;
      this.cost = cost;
   }

refillSlot() which adds the item into the itemList ArrayList up to MAX_ITEMS
Code:
   public void refillSlot(Item item) {
      for(int i=0;i<=MAX_ITEMS;i++)
         this.itemList.add(item);
   }

So I can't see it :)

I blame my cold


EDIT: GAH nevermind! Hadn't initiliased the itemList ArrayList, new constructor for Slot

Code:
   public Slot(String slotID) {
      this.slotID = slotID;
      this.itemList = new ArrayList<Item>(MAX_ITEMS);
   }

Just a little time waster if you're interested, run up to Christmas is VERY quiet in work, need something to fill the time so thought I might as well practice random Java stuff. All hand coded to get used to understanding it proper :)

vmachinecg2.jpg
 
Last edited:
Back
Top Bottom