Results 1 to 3 of 3
Thread: NullPointerException error
- 09-29-2010, 02:02 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 4
- Rep Power
- 0
NullPointerException error
Hello,
I have 2 classes: Point and Hook. The Point class has 2 fields: x and y.
In the Hook class I can create an array of Points. However I get a NullPointerException error when assigning a value to this x value via the Hook class.
here is the Point class:
here is the hook class:Java Code:public class point{ int x; int y; //default constructor point(){ x = 0; y = 0; }
This line "array1[0].x = 90;" gives me the exception error.Java Code:public class Hook{ //declare array point array1[]; //make the default constructor Hook(){ //make a new array existing out of points from the point class. array1 = new point [3]; array1[0].x = 90; }
According to this exception 'array1[0].x ' does not exist. Can someone explain to me what is going wrong here?
- 09-29-2010, 02:12 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
An array declaration creates an array of references, but, until you assign a value to these references, they all point to null. You need to add an array1[index] = new point(); before you start assigning the x and y values at that index.
- 09-29-2010, 02:31 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
java.lang.NullPointerException Error
By Manfizy in forum NetBeansReplies: 3Last Post: 02-24-2011, 06:27 AM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
awt TextField nullPointerException error
By k2k in forum AWT / SwingReplies: 3Last Post: 02-24-2009, 04:24 AM -
ERROR: nullPointerException in applet
By barney in forum Java AppletsReplies: 1Last Post: 08-07-2007, 07:11 AM -
ERROR: nullPointerException
By mathias in forum New To JavaReplies: 1Last Post: 08-05-2007, 06:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks