Results 1 to 2 of 2
- 07-13-2007, 10:12 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 44
- Rep Power
- 0
ArrayList: Exception in thread "main" java.lang.NullPointerException
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:
Java Code: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)
Any help just pointing out where I am going wrong would be great!Java Code: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; } }
Thanks.
- 07-16-2007, 06:32 AM #2
Member
- Join Date
- Jul 2007
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
By hemanthjava in forum AWT / SwingReplies: 3Last Post: 01-29-2008, 01:37 AM -
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM -
Exception in thread "main" java.lang.NoClassDefFoundError
By carl in forum New To JavaReplies: 2Last Post: 08-01-2007, 05:26 AM -
Exception in thread "main" java.lang.NoClassesDefFoundError
By tommy in forum New To JavaReplies: 1Last Post: 07-31-2007, 01:54 PM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks