I'm not sure why I am getting a null pointer exception. Or could someone point me in the right direction where to put a try-catch block perhaps. thanks
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
//write your code here
// CREATE OBJECTS
// three children
Human zach = new Human("Zach", true, 34);
Human henry = new Human("Henry", true, 2);
Human evie = new Human("Evie", false, 4);
// initialize an array and add kids to it
ArrayList<Human> grandkids = new ArrayList<Human>();
grandkids.add(henry);
grandkids.add(evie);
grandkids.add(zach);
// one father
Human ty = new Human("Ty", true, 34, grandkids);
// one mother.
Human kate = new Human("Kate", false, 35, grandkids);
// initialize array and add kids to it
ArrayList<Human> maleKids = new ArrayList<Human>();
maleKids.add(ty);
ArrayList<Human> femaleKids = new ArrayList<Human>();
femaleKids.add(kate);
// two grandfathers
Human clay = new Human("Clay", true, 70, maleKids);
Human rich = new Human("Rich", true, 77, femaleKids);
// two grandmothers
Human marty = new Human("Marty", false, 78, femaleKids);
Human debbie = new Human("Debbie", false, 65, maleKids);
// print out all objects using the toString() method
System.out.println(zach.toString());
System.out.println(henry.toString());
System.out.println(evie.toString());
System.out.println(ty.toString());
System.out.println(kate.toString());
System.out.println(marty.toString());
System.out.println(rich.toString());
System.out.println(clay.toString());
System.out.println(debbie.toString());
}
public static class Human {
//write your code here
// initialize variables
String name;
boolean sex;
int age;
ArrayList<Human> children;
// create constructor for everyone but children
public Human(String _name, boolean _sex, int _age, ArrayList<Human> _children)
{
this.name = _name;
this.sex = _sex;
this.age = _age;
this.children = _children;
}
// create constructor for children
public Human(String _name, boolean _sex, int _age)
{
this.name = _name;
this.sex = _sex;
this.age = _age;
}
public String toString() {
String text = "";
text += "Name: " + this.name;
text += ", sex: " + (this.sex ? "male" : "female");
text += ", age: " + this.age;
int childCount = this.children.size();
if (childCount > 0) {
text += ", children: " + 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.codegym.task.task08.task0824;
/*
Make a family
*/
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
//write your code here
// CREATE OBJECTS
// three children
Human zach = new Human("Zach", true, 34);
Human henry = new Human("Henry", true, 2);
Human evie = new Human("Evie", false, 4);
// initialize an array and add kids to it
ArrayList<Human> grandkids = new ArrayList<Human>();
grandkids.add(henry);
grandkids.add(evie);
grandkids.add(zach);
// one father
Human ty = new Human("Ty", true, 34, grandkids);
// one mother.
Human kate = new Human("Kate", false, 35, grandkids);
// initialize array and add kids to it
ArrayList<Human> maleKids = new ArrayList<Human>();
maleKids.add(ty);
ArrayList<Human> femaleKids = new ArrayList<Human>();
femaleKids.add(kate);
// two grandfathers
Human clay = new Human("Clay", true, 70, maleKids);
Human rich = new Human("Rich", true, 77, femaleKids);
// two grandmothers
Human marty = new Human("Marty", false, 78, femaleKids);
Human debbie = new Human("Debbie", false, 65, maleKids);
// print out all objects using the toString() method
System.out.println(zach.toString());
System.out.println(henry.toString());
System.out.println(evie.toString());
System.out.println(ty.toString());
System.out.println(kate.toString());
System.out.println(marty.toString());
System.out.println(rich.toString());
System.out.println(clay.toString());
System.out.println(debbie.toString());
}
public static class Human {
//write your code here
// initialize variables
String name;
boolean sex;
int age;
ArrayList<Human> children;
// create constructor for everyone but children
public Human(String _name, boolean _sex, int _age, ArrayList<Human> _children)
{
this.name = _name;
this.sex = _sex;
this.age = _age;
this.children = _children;
}
// create constructor for children
public Human(String _name, boolean _sex, int _age)
{
this.name = _name;
this.sex = _sex;
this.age = _age;
}
public String toString() {
String text = "";
text += "Name: " + this.name;
text += ", sex: " + (this.sex ? "male" : "female");
text += ", age: " + this.age;
int childCount = this.children.size();
if (childCount > 0) {
text += ", children: " + this.children.get(0).name;
for (int i = 1; i < childCount; i++) {
Human child = this.children.get(i);
text += ", " + child.name;
}
}
return text;
}
}
}