Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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-02-2007, 08:31 AM
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
java.lang.NullPointerException at autonomic.PbilMain.main(PbilMain.java:12)
I have a class called Product. I would like to refer to objects of the class by index. eg.

Code:
public class PbilMain { public static void main(String[] args) { Product p[] = new Product[4]; p[1].description = "Another trial"; p[2].description = "Yet another trial"; p[3].description = ""; p[4].description = ""; } }
Can anybody spot the problem. On running it, I get "Exception in thread "main" java.lang.NullPointerException at autonomic.PbilMain.main(PbilMain.java:12)"

Thanks.

Felissa
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-02-2007, 08:33 AM
Member
 
Join Date: Jun 2007
Posts: 92
Daniel is on a distinguished road
You declared space for the array but you still need to declare space for each element in the array. Also, remember that arrays are zero based. That means the first element in the array is [0] and the last is, in your case, [3]. Something like:

Code:
public class PbilMain { public static void main(String[] args) { Product p[] = new Product[4]; for( int i = 0; i < p.length; i++ ) p[i] = new Product(); p[0].description = "Another trial"; p[1].description = "Yet another trial"; p[2].description = ""; p[3].description = ""; } }
Greetings.

Daniel
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-02-2007, 08:36 AM
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
Thank you, I did what you said to me and it went out for me.

Thank you very much again.

Felissa
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
java.lang.NullPointerException stevemcc AWT / Swing 2 02-08-2008 11:01 AM
java.lang.NullPointerException ravian New To Java 1 01-13-2008 09:39 PM
exception in thred main java.lang.nosuchmethoderror: main fernando Java Applets 1 08-06-2007 11:11 AM
ArrayList: Exception in thread "main" java.lang.NullPointerException susan New To Java 1 07-16-2007 08:32 AM
java.lang.NullPointerException Felissa Advanced Java 1 07-05-2007 08:02 AM


All times are GMT +3. The time now is 10:27 AM.


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