Получаю ошибку NullPointerException, что не так? Вроде, все так хорошо шло(
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
//напишите тут ваш код
Human child1 = new Human("Ребенок1", true, 10);
Human child2 = new Human("Ребенок2", true, 12);
Human child3 = new Human("Ребенок3", true, 15);
Human father = new Human("Отец", true, 50, child1, child2, child3);
Human mother = new Human("Мать", false, 50, child1, child2, child3);
Human grandfather1 = new Human("Дед1", true, 99, father);
Human grandfather2 = new Human("Дед2", true, 90, mother);
Human grandmother1 = new Human("Бабка1", false, 99, father);
Human grandmother2 = new Human("Бабка2", false, 99, mother);
System.out.println(child1);
System.out.println(child2);
System.out.println(child3);
System.out.println(father);
System.out.println(mother);
System.out.println(grandfather1);
System.out.println(grandfather2);
System.out.println(grandmother1);
System.out.println(grandmother2);
}
public static class Human {
//напишите тут ваш код
String name;
boolean sex;
int age;
ArrayList<Human> children;
public Human(String name, boolean sex, int age, Human... children){
this.name = name;
this.sex = sex;
this.age = age;
ArrayList<Human> list_children = new ArrayList<>();
for(Human human: children){
list_children.add(human);
}
this.children = list_children;
}
public Human(String name, boolean sex, int age){
this.name = name;
this.sex = sex;
this.age = age;
}
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;
}
}
}