添加到ArrayList时Java NullPointerException? [英] Java NullPointerException when adding to ArrayList?
本文介绍了添加到ArrayList时Java NullPointerException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码抛出 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);
为什么会这样?
推荐答案
根据你提供的部分代码,看起来你还没有初始化myPolygon
based on the parts of the code you provided, it looks like you haven't initialized myPolygon
这篇关于添加到ArrayList时Java NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文