Незнаю я в общем, подскажите пожалуйста что нибудь. Запарился переписывать все.
Вылетает NullPointerException
package com.javarush.task.task08.task0824;
import com.sun.org.apache.xerces.internal.impl.dv.xs.SchemaDVFactoryImpl;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Collections;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
ArrayList<Human> kids = new ArrayList<>();
ArrayList<Human> mom = new ArrayList<>();
ArrayList<Human> dad = new ArrayList<>();
ArrayList<Human> old = new ArrayList<>();
Human kids1 = new Human("kids1",true,2);
Human kids2 = new Human("kids1",true,3);
Human kids3 = new Human("kids1",true,4);
kids.add(kids1);
kids.add(kids2);
kids.add(kids3);
/*kids.add(new Human("kids1",true,1));
kids.add(new Human("kids2",true,2));
kids.add(new Human("kids3",true,3));*/
Human father = new Human("father",true,34,kids);
Human mother = new Human("mother",false,32,kids);
/*Human father = new Human("parents1",true,29,kids);
Human mother = new Human("parents2",false,25, kids);*/
dad.add(father);
mom.add(mother);
Human ded1 = new Human("ded1",true,66,dad);
Human ded2 = new Human("ded2",true,95,mom);
Human baba1 = new Human("baba1",false,63,dad);
Human baba2 = new Human("baba2",false,50,mom);
old.add(ded1);
old.add(ded2);
old.add(baba1);
old.add(baba2);
/*old.add(new Human("ded1",true,66,dad));
old.add(new Human("ded2",true,95,mom));
old.add(new Human("baba1",false,63,dad));
old.add(new Human("baba2",false,50,mom));*/
ArrayList<Human> family = new ArrayList<>();
family.addAll(kids);
family.addAll(mom);
family.addAll(dad);
family.addAll(old);
for (Human x : family)
{
System.out.println(x);
}
}
public static class Human {
String name;
boolean sex;
int age;
ArrayList<Human> children;
Human (String name, boolean sex, int age)
{
this.name = name;
this.sex = sex;
this.age = age;
}
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;
}
}
}