Коллеги! Глаз устал , замылился, в упор не вижу причину ругани валидатора. Если не сложно, подскажите , что железяке не так, ТЗ то в соответствии)
package com.javarush.task.task08.task0824;
/*
Собираем семейство
*/
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
//напишите тут ваш код
ArrayList<Human> child = new ArrayList<>();
// список детей
Human child1 = new Human("child1", true, 30 );
Human child2 = new Human("child2", false, 30);
Human child3 = new Human("child3", true, 30);
//создали 3х детей типа "Human" без 4го параметра , в него передали нулл
child.add(child1);
child.add(child2);
child.add(child3);
//Добавили детей в список "child"
System.out.println(child1);
System.out.println(child2);
System.out.println(child3);
// Вывели на экран детей (объекты)
ArrayList<Human> famo = new ArrayList<>();
//Создали список куда поместим объекты "Отец" и "Мать"
Human father = new Human("father", true, 50, child);
Human mother = new Human("mother", false, 50, child);
//Создали объекты father, mother, где в 4й параметр внесли ссылку на список детей
famo.add(father);
famo.add(mother);
//Добавили в соответствующий список объекты father, и mother
System.out.println(father);
System.out.println(mother);
//Вывели на экран
ArrayList<Human> granies = new ArrayList<>();
//Создали список стариков
Human grandFather1 = new Human("grandFather1", true, 70, famo);
Human grandFather2 = new Human("grandFather2", true, 70, famo);
Human grandMother1 = new Human("grandMother1", false, 70, famo);
Human grandMother2 = new Human("grandMother2", false, 70, famo);
//Создали стариков, где в 4й параметр передали ссылку на список "Старческих детей")))
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 = new ArrayList<Human>();
//4 поля класса ( они ж переменные класса)
public Human(String name, boolean sex, int age, ArrayList<Human> children) {
// Конструктор класса
this.name = name;
this.sex = sex;
this.age = age;
this.children = children;
}
public Human(String name, boolean sex, int age) {
// Конструктор класса
this.name = name;
this.sex = sex;
this.age = age;
this.children = new ArrayList<>();
}
public String toString() { // переопределенный метод
String text = "";
text += "Имя: " + this.name;
text += ", пол: " + (this.sex ? "мужской" : "женский");
text += ", возраст: " + this.age;
int childCount = this.children.size(); // а вот тут компилятор ругается
//NullPointerException
// починилось добавлением конструктора без 4го параметра + в @children передаётся новый пустой список
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;
}
}
}