Results 1 to 3 of 3
- 07-02-2007, 06:31 AM #1
Member
- Join Date
- Jun 2007
- Posts
- 95
- Rep Power
- 0
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.
Can anybody spot the problem. On running it, I get "Exception in thread "main" java.lang.NullPointerException at autonomic.PbilMain.main(PbilMain.java:12)"Java 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 = ""; } }
Thanks.
Felissa :p
- 07-02-2007, 06:33 AM #2
Member
- Join Date
- Jun 2007
- Posts
- 91
- Rep Power
- 0
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:
Greetings.Java 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 = ""; } }
Daniel:o
- 07-02-2007, 06:36 AM #3
Member
- Join Date
- Jun 2007
- Posts
- 95
- Rep Power
- 0
Similar Threads
-
java.lang.NullPointerException
By stevemcc in forum AWT / SwingReplies: 2Last Post: 02-08-2008, 09:01 AM -
java.lang.NullPointerException
By ravian in forum New To JavaReplies: 1Last Post: 01-13-2008, 07:39 PM -
exception in thred main java.lang.nosuchmethoderror: main
By fernando in forum Java AppletsReplies: 1Last Post: 08-06-2007, 09:11 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM -
java.lang.NullPointerException
By Felissa in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 06:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks