尽管对象似乎存在,但我的代码仍然抛出了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