I am writing a program in Java, in which I define a class
class Point
{
double x;
double y;
}
Then in a method, I define an array of points, as follows:
Point[] line = new Point[6];
In the same method, I have the line
line[SampleSize - i + 1].x = i;
The first time this statement is hit, the value of its array index is 1; but the program throws a null pointer exception at this point.
This would seem like the correct way to index the field of an object within an array of objects. What am I doing wrong?
Thanks in advance for any suggestions.
John Doner
You have to initialize the value before accessing it:
line[SampleSize - i + 1] = new Point();
That's because you have not created the points to put in the array
for (int index = 0; index < line.length; index++)
{
line[index] = new Point();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With