Mam interfejs Executable
, posiadający m. in. metodę public String getCode()
. Posiadam dwie klasy implementujące ten interfejs:
-
Code
-
Block
Fragment klasyCode
- konstruktor i metodagetCode()
:
private String code;
public Code(String code) {
this.code = code;
}
public String getCode() {
return this.code;
}
Fragment klasy Block
zawierającej zbiór obiektów implementujących Executable
:
private Executable[] ex;
public Block(Executable[] ex) {
this.ex = ex;
}
public String getCode() {
String ret = "";
for (int i = 0; i < ex.length; i++) {
ret += (ex[i].getCode() + "\n"); //Ta linia zwraca NullPointerException
}
return ret;
}
Tworzę tablicę 3 obiektów klasy Code
i sprawdzam, następnie tworzę blok i otrzymuję wyjątek:
Code[] codes = new Code[3];
codes[0] = "{";
codes[1] = "test";
codes[2] = "test2";
for (int i = 0; i < codes.length; i++) {
System.out.println(codes[i].getCode());
}
Block b = new Block(codes);
System.out.println(b.getCode()); //NullPointerException