Если проверять так, как сейчас написан код, то выдает ошибку, что обращаюсь к неинициализированному объекту в момент когда проверяю есть ли дети (в строке 71). В моем понимании, при передаче конструктору (второму, который не содержит списка детей) первых двух объектов (детей), у которых нет детей - в строке 71 должно быть false и далее код не выполняется, но при этом список как бы есть в самом классе Human и его длина равна нулю. В каком месте мои суждения становятся неверными?
package com.javarush.task.task08.task0824;
import java.util.*;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
// ArrayList<Human> ch = new ArrayList<Human>();
Human sun = new Human ("Ivan", true, 13, /*ch*/);//создаем детей
Human dochka_1 = new Human ("Maria", false, 12, /*ch*/);
Human dochka_2 = new Human ("Masha", false, 10, /*ch*/);
ArrayList<Human> children = new ArrayList<Human>();
children.add(sun);
children.add(dochka_1);
children.add(dochka_2);
Human otec = new Human ("Otec", true, 37, children);//создаем родителей
Human mama = new Human ("Mama", false, 36, children);
ArrayList<Human> PAPA = new ArrayList<Human>();
ArrayList<Human> MAMA = new ArrayList<Human>();
PAPA.add(otec);
MAMA.add(mama);
Human ded_1 = new Human ("Ded_1", true, 65, PAPA);//создаем бабушку, дедушку со стороны папы
Human babushka_1 = new Human ("Babushka_1", false, 65, PAPA);
Human ded_2 = new Human ("Ded_2", true, 65, MAMA);//создаем бабушку, дедушку со стороны мамы
Human babushka_2 = new Human ("Babushka_2", false, 65, MAMA);
System.out.println(sun);
System.out.println(dochka_1);
System.out.println(dochka_2);
System.out.println(otec);
System.out.println(mama);
System.out.println(ded_1);
System.out.println(babushka_1);
System.out.println(ded_2);
System.out.println(babushka_2);
}
public static class Human {
String name;
boolean sex;
int age;
ArrayList <Human> children ;
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;
}
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;
}
}
}package com.javarush.task.task08.task0824;
import java.util.*;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
ArrayList<Human> ch = new ArrayList<Human>();
Human sun = new Human ("Ivan", true, 13, ch);//создаем детей
Human dochka_1 = new Human ("Maria", false, 12);
Human dochka_2 = new Human ("Masha", false, 10);
ArrayList<Human> children = new ArrayList<Human>();
children.add(sun);
children.add(dochka_1);
children.add(dochka_2);
Human otec = new Human ("Otec", true, 37, children);//создаем родителей
Human mama = new Human ("Mama", false, 36, children);
ArrayList<Human> PAPA = new ArrayList<Human>();
ArrayList<Human> MAMA = new ArrayList<Human>();
PAPA.add(otec);
MAMA.add(mama);
Human ded_1 = new Human ("Ded_1", true, 65, PAPA);//создаем бабушку, дедушку со стороны папы
Human babushka_1 = new Human ("Babushka_1", false, 65, PAPA);
Human ded_2 = new Human ("Ded_2", true, 65, MAMA);//создаем бабушку, дедушку со стороны мамы
Human babushka_2 = new Human ("Babushka_2", false, 65, MAMA);
System.out.println(sun);
//System.out.println(dochka_1);
//System.out.println(dochka_2);
System.out.println(otec);
System.out.println(mama);
System.out.println(ded_1);
System.out.println(babushka_1);
System.out.println(ded_2);
System.out.println(babushka_2);
}
public static class Human {
String name;
boolean sex;
int age;
ArrayList <Human> children ;
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;
}
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;
}
}
}