2014-01-12 22 views
0

在構造函數中,我試圖從Point2D數組構建一個Point2D.Double數組。
基本上我想給座標添加座標。
我這樣做:Java:構建Point2D.Double的數組

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()); 
    } 
} 

但我發現了一個NullPointerException。

座標數組(pointArray)來自練習的給定代碼。所以我猜猜這個錯誤就在我身上。

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); 
+1

您傳遞給此構造函數的數組中可能有一個空值。你能告訴我們你創建'EmbeddedGraph'的代碼嗎? –

+0

添加到最初的問題 – sebastian

+0

好吧,現在哪條線引發空指針異常?你的堆棧跟蹤會告訴你這一點。 –

回答

3

您正試圖填補points[]陣列時,它是空的。 你應該先做到這一點:

`points = new Point2D[pointArray.length]` 

(如果它不是在做super());

+0

是的。我錯過了。謝謝。 – sebastian