Java的NullPointerException异常时增加ArrayList的? [英] Java NullPointerException when adding to ArrayList?
本文介绍了Java的NullPointerException异常时增加ArrayList的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的code抛出NullPointerException异常,即使对象似乎正常存在。
My code is throwing a NullPointerException, even though the object seems to properly exist.
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);
为什么会出现这种情况?
Why is this happening?
推荐答案
根据您所提供的code的部分,它看起来像你还没有初始化 myPolygon
based on the parts of the code you provided, it looks like you haven't initialized myPolygon
这篇关于Java的NullPointerException异常时增加ArrayList的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文