I keep getting Null Pointer Exception.
What am I doing wrong?
package com.codegym.task.task06.task0621;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Cat relations
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String grandfatherName = reader.readLine();
Cat catGrandfather = new Cat(grandfatherName);
String grandmotherName = reader.readLine();
Cat catGrandMother = new Cat(grandmotherName);
String fatherName = reader.readLine();
Cat catFather = new Cat(fatherName, catGrandMother, catGrandfather);
String motherName = reader.readLine();
Cat catMother = new Cat(motherName, catGrandMother, catGrandfather);
String sonName = reader.readLine();
Cat catSon = new Cat(sonName, catMother, catFather);
String daughterName = reader.readLine();
Cat catDaughter = new Cat(daughterName, catMother, catFather);
System.out.println(catMother);
System.out.println(catDaughter);
System.out.println(catSon);
System.out.println(catFather);
System.out.println(catGrandMother);
System.out.println(catGrandfather);
}
public static class Cat {
private String name;
private Cat parent;
private Cat mom;
private Cat dad;
Cat(String name) {
this.name = name;
}
Cat(String name, Cat parent) {
this.name = name;
this.parent = parent;
}
Cat(String name, Cat mom, Cat dad){
this.name = name;
this.mom = mom;
this.dad = dad;
}
@Override
public String toString() {
if (mom == null){
return "The cat's name is " + name + ", no mother, " + dad.name + " is the father";
}
else if (dad == null){
return "The cat's name is " + name + ", " + mom.name + " is the mother, no father ";
}
else if (mom == null && dad == null){
return "The cat's name is " + name + ", no mother, no father ";
}
else {
return "The cat's name is " + name + ", " + mom.name + " is the mother, " + dad.name + " is the father ";
}
}
}
}