即使對象似乎正確存在,我的代碼仍然拋出一個NullPointerException異常。添加到ArrayList時Java NullPointerException?
public class IrregularPolygon {
private ArrayList<Point2D.Double> myPolygon;
public void add(Point2D.Double aPoint) {
System.out.println(aPoint); // Outputs Point2D.Double[20.0, 10.0]
myPolygon.add(aPoint); // NullPointerException gets thrown here
}
}
// Everything below this line is called by main()
IrregularPolygon poly = new IrregularPolygon();
Point2D.Double a = new Point2D.Double(20,10);
poly.add(a);
這是怎麼發生的?
這是真的嗎?我不這麼認爲,因爲如果你將它初始化爲一個'ArrayList',那麼你不需要在參數的另一端指定一個類型。 – Ajay 2016-12-31 17:33:25