java.lang.NullPointerException
at com.javarush.task.task08.task0824.Solution$Human.toString(Solution.java:80)
at com.javarush.task.task08.task0824.Solution.main(Solution.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.
В связи с чем выходит такая ошибка? Как это устранить?
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
import java.util.List;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
Human chilgren1 = new Human("hhhhd",false,10);
Human chilgren2 = new Human("hhlkllhhd",false,10);
Human chilgren3 = new Human("hhhhhhhhd",false,10);
ArrayList<Human> children = new ArrayList<>();
children.add(chilgren1);
children.add(chilgren2);
children.add(chilgren3);
Human father = new Human("gfhbnfb", true,30,children);
Human mother = new Human("fvdfvdbdvvxc", false,28, children);
ArrayList<Human> fatherList = new ArrayList<>();
ArrayList<Human> motherList = new ArrayList<>();
fatherList.add(father);
motherList.add(mother);
Human grandFather1 = new Human("hgg,n,,nhg",true, 50,fatherList);
Human grandFather2 = new Human("gfggf",true,60,motherList);
Human grandMother1 = new Human("ggm...m.ggh", false,50, fatherList);
Human grandMother2 = new Human("vghfdfv", false,55,motherList);
for( int i = 0;i<3;i++){
System.out.println(children.get(i).toString());
}
System.out.println(father.toString());
System.out.println(mother.toString());
System.out.println(grandFather1.toString());
System.out.println(grandFather2.toString());
System.out.println(grandMother1.toString());
System.out.println(grandMother2.toString());
}
public static class Human {
String name;
boolean sex;
int age;
List<Human> children;
public Human(String name, boolean sex, int age, List<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;
}
}
}