Hi all,
I cannot display last two lines and get NullPointerException. Any clue please?
Thanks,
Roman
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,catGrandfather);
String motherName = reader.readLine();
Cat catMother = new Cat (motherName, catGrandmother);
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(catGrandfather);
System.out.println(catGrandmother);
System.out.println(catFather);
System.out.println(catMother);
System.out.println(catSon);
System.out.println(catDaughter);
}
public static class Cat {
private String name;
private String name2;
private Cat grandparent;
private Cat mother;
private Cat father;
Cat(String name) {
this.name = name;
}
Cat(String name, Cat grandparent) {
this.name = name;
this.grandparent = grandparent;
}
Cat(String name2, Cat mother, Cat father) {
this.name2 = name2;
this.mother = mother;
this.father = father;
}
@Override
public String toString() {
/*
if (name.equals("Grandfather Tiger")) {
return "The cat's name is " + name + ", no mother, no father";
}else if(name.equals("Grandmother Puss")) {
return"The cat's name is "+name +", no mother, no father";
}else if (grandparent.name.equals("Grandfather Tiger")){
return "The cat's name is " + name + ", no mother, " + grandparent.name + " is the father";
} else if (grandparent.name.equals("Grandmother Puss")){
return "The cat's name is " + name + ", " + grandparent.name + " is the mother, no father";
} else if (name.equals("Son Simba")) {
return "The cat's name is " + name + ", " + mother.name + " is the mother, " + father.name + " is the father";
} else if (name.equals("Daughter Coco")) {
return "The cat's name is " + name + ", " + mother.name + " is the mother, " + father.name + " is the father";
} else {
return "";
}*/
if (grandparent == null && mother == null)
return "The cat's name is " + name + ", no mother, no father";
else if (grandparent.name.equals("Grandfather Tiger")){
return "The cat's name is " + name + ", no mother, " + grandparent.name + " is the father";
} else if (grandparent.name.equals("Grandmother Puss")){
return "The cat's name is " + name + ", " + grandparent.name + " is the mother, no father";
} else if (name2.equals("Son Simba")) {
return "The cat's name is " + name2 + ", " + mother.name + " is the mother, " + father.name + " is the father";
} else if (name.equals("Daughter Coco")) {
return "The cat's name is " + name + ", " + mother.name + " is the mother, " + father.name + " is the father";
} else {
return "";
}
}
}
}
/*
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String motherName = reader.readLine();
Cat catMother = new Cat(motherName);
String daughterName = reader.readLine();
Cat catDaughter = new Cat(daughterName, catMother);
System.out.println(catMother);
System.out.println(catDaughter);
}
public static class Cat {
private String name;
private Cat parent;
Cat(String name) {
this.name = name;
}
Cat(String name, Cat parent) {
this.name = name;
this.parent = parent;
}
@Override
public String toString() {
if (parent == null)
return "The cat's name is " + name + ", no mother ";
else
return "The cat's name is " + name + ", " + parent.name + " is the mother";
}
}
*/