Пристрелите меня кто-нибудь. У меня уже глаз дёргается.
NullPointerException & выводить детей не хочет
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
ArrayList<Human> children = new ArrayList<>();
Human son3;
Human son1;
Human son2;
children.add(son3 = new Human("Kostya", true, 34, null));
children.add(son1 = new Human("Petya", true, 34, null));
children.add(son2 = new Human("Vasya", true, 34, null));
ArrayList<Human> parents = new ArrayList<>();
Human mother;
parents.add(mother = new Human("Katya", false, 52, children));
ArrayList<Human> parent = new ArrayList<>();
Human father;
parent.add(father = new Human("Ivan", true, 56, children));
Human grandmother1 = new Human("Vera", false, 84, parents);
Human grandmother2 = new Human("Nadia", false, 79, parent);
Human grandfather1 = new Human("Pasha", true, 90, parents);
Human grandfather2 = new Human("Vitya", true, 87, parent);
System.out.println(grandmother1.toString());
System.out.println(grandmother2.toString());
System.out.println(grandfather1.toString());
System.out.println(grandfather2.toString());
System.out.println(mother.toString());
System.out.println(father.toString());
System.out.println(son3.toString());
System.out.println(son1.toString());
System.out.println(son2.toString());
}
public static class Human {
public boolean sex;
public String name;
public int age;
public 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;
}
}
}