I am initializing an Object(Bateau), which contains within it an array of objects (Element) called elements.
Upon calling the constructor, I want to create several elements for a "bateau" object.
The number of elements vary depending on which kind of "bateau" it is because "Bateau" has 3 sub-classes which determine the number of elements to initialize.
I get a NPE when I try to add a new Element to elements, can anyone help me resolve this issue ?
public class Bateau {
private boolean horizontal = false;
protected Element[] elements;
public Bateau(int pX, int pY, boolean horizontal, int nombreElements) {
this.horizontal = horizontal;
for (int i = 0; i < nombreElements; i++) {
if (this.horizontal) {
//NullPointerException elements[i] not initialized ?
this.elements[i] = new Element((pX + i), pY);
} else {
this.elements[i] = new Element(pX, (pY + i));
}
}
}}