0

Hi I have created a toStringmethod in one of my classes which can be seen below. 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.

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.

Tester Class: package Practical5;

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

4
  • 2
    Post a complete, minimal example reproducing the problem. Your posted code has many irrelevant details, but lacks critical information (like, the Student class being printed, for example). The question should also contain the output you expect, and the output you get. Commented Apr 9, 2016 at 14:36
  • If it is returning null values, it must be throwing a NPE, post the stack trace please Commented Apr 9, 2016 at 14:38
  • Now post Person, since that's what is returning null. Commented Apr 9, 2016 at 14:43
  • The problem seems to be in the class Person, post it as well Commented Apr 9, 2016 at 14:43

3 Answers 3

2
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.

Sign up to request clarification or add additional context in comments.

Comments

1

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;

}

Comments

0

Look at the constructor in person and in student, Should use the parameters in the method header.

super(name,dob,address)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.