Выдает эту ошибку
java.lang.NullPointerException
at com.javarush.task.task08.task0824.Solution$Human.toString(Solution.java:70)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at com.javarush.task.task08.task0824.Solution.main(Solution.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.javarush.task.common.model.execution.ExecuteService.callMainMethod(ExecuteService.java:78)
at com.javarush.task.common.model.execution.ExecuteService.execute(ExecuteService.java:102)
at com.javarush.task.app.execution.ExecutionService.execute(ExecutionService.java:110)
at com.javarush.task.app.execution.ExecutionService.main(ExecutionService.java:254)
at com.javarush.task.app.MainApplication.main(MainApplication.java:49)
package com.javarush.task.task08.task0824;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) throws IOException {
//напишите тут ваш код
ArrayList<Human> listforpareants = new ArrayList<Human>();
ArrayList<Human> listForGrandParents = new ArrayList<Human>();
ArrayList<Human> listForGrandParents2 = new ArrayList<Human>();
ArrayList<Human> all = new ArrayList<Human>();
Human son = new Human("Борис", true, 12);
Human douther = new Human("Инна", false, 10);
Human son2 = new Human("Глеб", true, 14);
listforpareants.add(son);
listforpareants.add(douther);
listforpareants.add(son2);
Human father = new Human ("Василий", true, 38, listforpareants);
Human mother = new Human ("Анастасия", false, 36, listforpareants);
listForGrandParents.add(father);
listForGrandParents2.add(mother);
Human grandfather = new Human("Алеша", true, 63, listForGrandParents);
Human grandmother = new Human("Валентина", false, 62, listForGrandParents);
Human grandfather2 = new Human("Иван", true, 61, listForGrandParents2);
Human grandmother2 = new Human("Дарья", false, 60, listForGrandParents2);
all.addAll(listforpareants);
all.addAll(listForGrandParents);
all.addAll(listForGrandParents2);
all.add(grandmother);
all.add(grandfather);
all.add(grandmother2);
all.add(grandfather2);
Iterator <Human> iterator = all.iterator();
while(iterator.hasNext())
System.out.println(iterator.next());
}
public static class Human {
//напишите тут ваш код
public String name;
public boolean sex;
public int age;
ArrayList<Human> children;
public Human (String name, boolean sex, int age){
this.name = name;
this.age = age;
this.sex = sex;
}
public Human (String name, boolean sex, int age, ArrayList<Human> children){
this.name = name;
this.age = age;
this.sex = sex;
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;
}
}
}