sebastian
sebastian

Reputation: 488

Java: Building an array of Point2D.Double

In a constructor I am trying to build an array of Point2D.Double from an Point2D array.
Basically I want to add coordinates to a graph.
I did this:

private Point2D.Double [] points;

public EmbeddedGraph(Point2D[] pointArray){
    super(pointArray.length);
    for (int i=0; i<pointArray.length; i++){
    points[i] = new Point2D.Double();
    points[i].setLocation(pointArray[i].getX(), pointArray[i].getY());
    }
}

But I'm getting a NullPointerException.

The array of coordinates (pointArray) comes from the given code of the exercise. So I'm guessing the error is on my part.

Point2D[] coordinates = new Point2D[4];
coordinates[0] = new Point2D.Double(-14,0);
coordinates[1] = new Point2D.Double(0,10);
coordinates[2] = new Point2D.Double(0,-10);
coordinates[3] = new Point2D.Double(14,0);
EmbeddedGraph g = new EmbeddedGraph( coordinates );

Upvotes: 0

Views: 5205

Answers (1)

Samurai Girl
Samurai Girl

Reputation: 998

You are trying to fill points[] array when it is null. You should do this first:

`points = new Point2D[pointArray.length]` 

(in case it is not done in super());

Upvotes: 3

Related Questions