Mon code lance une exception NullPointerException, même si l'objet semble exister correctement.Java NullPointerException lors de l'ajout à ArrayList?
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);
Pourquoi cela se produit-il?
Est-ce encore vrai? Je ne pense pas, parce que si vous l'initialisez comme un 'ArrayList' alors vous n'avez plus besoin de spécifier un type de l'autre côté de l'argument. – Ajay