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);
您傳遞給此構造函數的數組中可能有一個空值。你能告訴我們你創建'EmbeddedGraph'的代碼嗎? –
添加到最初的問題 – sebastian
好吧,現在哪條線引發空指針異常?你的堆棧跟蹤會告訴你這一點。 –