0
객체 (Bateau)를 초기화하는 중입니다.이 객체에는 요소라는 객체의 배열 (Element)이 들어 있습니다.자바 - 객체를 객체 배열에 추가 할 수 없습니다 - NullPointerException
생성자를 호출 할 때 "batoau"개체에 대한 여러 요소를 만들고 싶습니다.
"Bateau"에는 초기화 할 요소의 수를 결정하는 3 개의 하위 클래스가 있기 때문에 요소의 수는 "batoau"의 종류에 따라 다릅니다.
요소에 새 요소를 추가하려고하면 NPE가 표시됩니다.이 문제를 해결하는 데 도움을 줄 수 있습니까? 디버거에서
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));
}
}
}}
와 함께 초기화 것을 볼 수있을 것인가? 몇 가지 읽기 : http://stackoverflow.com/questions/25761646/initialize-java-array-vs-other-initialization –
추가 : elements = new Element [nombreElements]; 내 생성자. – beln
http://stackoverflow.com/q/27180473/573032 –