简体   繁体   English

打印出arrayList时是否为空值?

[英]Null values when printing out arrayList?

Hi I have created a toStringmethod in one of my classes which can be seen below. 嗨,我在我的一个类中创建了一个toString方法,如下所示。 Student Class: 学生班:

package Practical5;

public class Student extends Person {

    //instance variables
    private static int MAX_MODULES = 6;
    private StudentMode modeOfStudy;
    private boolean studentLoan;
    private int numEnrolledModules;

    //constructor
    public Student(String name, String dob, Address address, StudentMode modeOfStudy, boolean studentLoan) {
        super(name, dob, address);
    this.modeOfStudy = modeOfStudy;
    this.studentLoan = studentLoan;
    this.numEnrolledModules = 0;
    }

    //accessors & mutators
    public StudentMode getMode() {
        return modeOfStudy;
    }

    public boolean isStudentLoan() {
        return studentLoan;
    }

    public int getNumEnrolledModules() {
        return numEnrolledModules;
    }

    public void setMode(StudentMode modeOfStudy) {
        this.modeOfStudy = modeOfStudy;
    }

    public void setStudentLoan(boolean studentLoan) {
        this.studentLoan = studentLoan;
    }

    public void setNumEnrolledModules(int numEnrolledModules) {
        this.numEnrolledModules = numEnrolledModules;
    }

    @Override
    public void purchaseParkingPass() {
        System.out.println(getName() + " just purchased a parking pass with student discount.");
    }

    @Override
    public void addModule(String moduleCode) {
        if (getNumEnrolledModules() < MAX_MODULES) {
            System.out.println(getName() + " successfully registered for the module: " + moduleCode);
        }
        else {
            System.out.println("You are unable to register for " + moduleCode + " as the maximum number of permitted module enrolments has been reached.");
        }

    }

    public String toString() {
        return "Student [ ID: " + getId() + "; Name: " + getName() + 
                "; DOB: " + getDob() + "; Study Mode: " + getMode() +
                "; Number of Enrolled Modules: " + getNumEnrolledModules();
    }
}

Person Class: 人员类别:

package Practical5;

public abstract class Person {


    //instance variables
    private static int LAST_ID = 1000 + 1;
    private int id;
    private String name;
    private String dob;
    private Address address;

    //constructor
    public Person(String name, String dob, Address address) {
        super();
        LAST_ID ++;
        this.id = LAST_ID;

    }

    //accessors & mutators
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDob() {
        return dob;
    }

    public Address getAddress() {
        return address;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    //methods
    public abstract void purchaseParkingPass();
    public abstract void addModule(String moduleCode);



}

I then created a tester class and created a new ArrayList and added these elements to it. 然后,我创建了一个测试器类,并创建了一个新的ArrayList并将这些元素添加到其中。

I then created a for loop in order to loop through each element and call the toString method to print out the details of each element but it is returning null values. 然后,我创建了一个for循环,以循环遍历每个元素,并调用toString方法以打印出每个元素的详细信息,但它返回的是空值。

Tester Class: package Practical5; 测试人员类别:实用软件包5;

import java.util.ArrayList;
import java.util.Scanner;



public class UIS_Tester {

    public static void main(String[] args) {

        Student student1 = new Student("James Black", "07/09/1995" , new Address("Wheeler's Road",10,"Belfast", "BT12 5EG", "Co.Antrim"),StudentMode.Fulltime, false);
        Student student2 = new Student("Adam Smith", "12/11/1979" , new Address("Ivy Hill",67,"Belfast", "BT17 7BN", "Co.Antrim"),StudentMode.Parttime, true);

        ArrayList<Person> uniPeople = new ArrayList<Person>();

        uniPeople.add(student1);
        uniPeople.add(student2);

        printMenu(uniPeople);



    }


    public static void printAllDetails(ArrayList<Person> uniPeople) {
        for (int i = 0; i < uniPeople.size(); i++) {
            System.out.println(uniPeople.get(i).toString());
        }
    }
}

Output: 输出:

Student [ ID: 1002; Name: null; DOB: null; Study Mode: Fulltime; Number of Enrolled Modules: 0
Student [ ID: 1003; Name: null; DOB: null; Study Mode: Parttime; Number of Enrolled Modules: 0

Can anyone help me with this problem? 谁能帮助我解决这个问题? Thanks 谢谢

public Person(String name, String dob, Address address) {
    super();
    LAST_ID ++;
    this.id = LAST_ID;
}

The constructor completely ignores its three arguments. 构造函数完全忽略其三个参数。 It doesn't assign them to the corresponding fields, so these fields keep their default value: null. 不会将它们分配给相应的字段,因此这些字段保留其默认值:null。

You have to store the name value in the constructor. 您必须将名称值存储在构造函数中。 Your version did not use the name value. 您的版本未使用名称值。

public Person(String name, String dob, Address address) {
    super();
    this.name = name; // <== important line
    this.dob = dob;  // <== important line
    this.address = address;  // <== important line
    LAST_ID ++;
    this.id = LAST_ID;

}

亲自和学生看构造函数,应使用方法标题中的参数。

super(name,dob,address)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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