简体   繁体   中英

Java - My super.toString is returning null

I'm having an issue where my toString method in Department is returning null values, it seems the super.toString isn't getting the values that are inputed. I'm not sure where the code went wrong, I know it's a lot to sort through, I tried to get rid of any code that isn't important. Thanks for taking a look. Sorry if I structured this wrong.

public class Person {
String name;
private int age;
private String ssn;
private boolean alive;

public Person(String name, int age, String ssn, boolean alive) {
    this.name = name;
    this.age = age;
    this.ssn = ssn;
    this.alive = alive;
}

public String toString() {
    return "Name: " + this.getName() + "\n" +
            "Age: " + this.getAge() + "\n" +
            "SSN: " + this.getSsn() + '\n' +
            "Alive: " + this.isAlive();
}
}

Teacher extends Person

public class Teacher extends Person {
private String id;
private int salary;
private int num_yr_prof;

public Teacher(String name, int age, String ssn, boolean alive,           String id, int salary, int num_yr_prof) {
    super(name, age, ssn, alive);
    this.id = id;
    this.salary = salary;
    this.num_yr_prof = num_yr_prof;
}
public Teacher(Teacher t) {
    this.id = t.id;
    this.salary = t.salary;
    this.num_yr_prof = t.num_yr_prof;
}

@Override
public String toString() {
    return super.toString() +
            "\nID: " + getId() +
            "\nSalary: $" + getSalary() +
            "\nYears as a prof: " + getNum_yr_prof();
}
}

This class uses an array of teachers

public class Department {
private String deptName;
private int numMajors;
private Teacher[] listTeachers;

public Department(String dN, int nM, Teacher[] lT) {
    this.deptName = dN;
    this.numMajors = nM;
    this.listTeachers = new Teacher[lT.length];
    for (int i = 0; i <lT.length ; i++) {
        listTeachers[i] = new Teacher(lT[i]);
    }
}

public String toString() {
    String output = "Department: ";
    output += "\n   Name: " + getDeptName();
    output += "\n   Majors: " + getNumMajors();
    output += "\n   Teachers: \n";
    for (int i = 0; i < listTeachers.length; i++) {
        output += listTeachers[i].toString(); //This is returning null values
    }
    return output;
}
}

The main method

public class lab4prt2 {
public static void main(String[] args) {
    Teacher[] teachers = new Teacher[]{
            new Teacher(
                    "Tracy",
                    36,
                    "39890280",
                    true,
                    "4859279",
                    100000,
                    5)
    };

    Department d1 = new Department("Math", 4, teachers);

    System.out.println(d1.toString());

}
}

At a first look, it seems to me that the Department constructor invokes the Teacher constructor with argument Teacher.

In this constructor, the super() is not invoked, and it doesn't compile, so I guess you probably missed something during your copy paste.

Anyway, I'm afraid that this is a very inefficient code because you re-create the Teacher objects starting from your array, and above all, you're losing part of the information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM