我有一个类ConvexHull,我必须为我的类实现。其中一个实例变量是一个包含对象Points的数组。添加对象到数组并获取NullPointerException
private Points[] points;
当我在构造函数中向数组中添加一个Point时,我得到一个NullPointerException。
public ConvexHull(int n) throws IllegalArgumentException
{
if (n < 1)
throw new IllegalArgumentException();
else {
Random rand = new Random();
for (int i=0; i<n; i++)
{
int x = rand.nextInt(101)-50;
int y = rand.nextInt(101)-50;
Point p = new Point(x,y);
this.points[i] = p; // NullPointerException is thrown.
this.numPoints = points.length;
}
}
}
基本上,我不知道为什么这不工作,需要一些帮助。