Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-13-2007, 11:12 PM
Member
 
Join Date: Jul 2007
Posts: 44
susan is on a distinguished road
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:

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)
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; } }
Any help just pointing out where I am going wrong would be great!

Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-16-2007, 07:32 AM
Member
 
Join Date: Jul 2007
Posts: 2
akaruppa is on a distinguished road
Hi,
I think the problem is in
public void createItemList()

In this method, you are defining a local variable
ArrayList itemList = new ArrayList();

Hence the member variable itemList is still empty..
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException hemanthjava AWT / Swing 3 01-29-2008 02:37 AM
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main barney New To Java 1 08-07-2007 08:10 AM
Exception in thread "main" java.lang.NoClassDefFoundError carl New To Java 2 08-01-2007 06:26 AM
Exception in thread "main" java.lang.NoClassesDefFoundError tommy New To Java 1 07-31-2007 02:54 PM
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException romina New To Java 1 07-25-2007 11:55 PM


All times are GMT +3. The time now is 02:27 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org