Не печатает детей. Вываливает ошибку. Почему?
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
import java.util.List;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
ArrayList<Human> kids = new ArrayList<Human>();
ArrayList<Human> father = new ArrayList<Human>();
ArrayList<Human> mother = new ArrayList<Human>();
kids.add(new Human("Nikola", false, 4));
kids.add(new Human("Daniela", false, 7));
kids.add(new Human("Alan", true, 10));
father.add(new Human("Egor", true, 34, kids));
mother.add(new Human("Nastja", false, 34, kids));
Human grandpa1 = new Human("Vasilij",true,78,father);
Human grandpa2 = new Human("Grigorij",true,80,mother);
Human grandma1 = new Human("Irina",false,65,father);
Human grandma2 = new Human("Svetlana",false,67,mother);
for (Human s : kids) System.out.println(s);
for (Human s : father) System.out.println(s);
for (Human s : mother) System.out.println(s);
System.out.println(grandpa1);
System.out.println(grandpa2);
System.out.println(grandma1);
System.out.println(grandma2);
}
public static class Human {
String name;
Boolean sex;
int age;
ArrayList<Human> children;
public Human(String name, Boolean sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public Human(String name, Boolean sex, int age, ArrayList<Human> children) {
this.name = name;
this.sex = sex;
this.age = age;
this.children = children;
}
public String toString() {
String text = "";
text += "Имя: " + this.name;
text += ", пол: " + (this.sex ? "мужской" : "женский");
text += ", возраст: " + this.age;
int childCount = this.children.size();
if (childCount > 0) {
text += ", дети: " + this.children.get(0).name;
for (int i = 1; i < childCount; i++) {
Human child = this.children.get(i);
text += ", " + child.name;
}
}
return text;
}
}
}
