2016-04-09 62 views
0

嗨我已經在我的一個類中創建了一個toString方法,可以在下面看到。 學生類別:打印出arrayList時爲空值嗎?

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類:

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); 



} 

然後我創建了一個測試類,並創建了一個新的ArrayList,並添加這些元素給它。

然後,我創建了一個for循環,以便遍歷每個元素並調用toString方法來打印出每個元素的詳細信息,但它返回空值。

測試儀類別: 包裝實用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()); 
     } 
    } 
} 

輸出:

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 

誰能幫我解決這個問題?謝謝

+2

發佈一個完整的,最小的例子來重現問題。您發佈的代碼有許多不相關的細節,但缺少關鍵信息(例如,正在打印的學生課程)。這個問題還應該包含你期望的輸出和你得到的輸出。 –

+0

如果它返回空值,它必須拋出一個NPE,請發佈堆棧跟蹤請 – Nadir

+0

現在發佈'Person',因爲這就是返回null。 –

回答

1

您必須在構造函數中存儲名稱值。您的版本沒有使用名稱值。

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; 

} 
+0

不確定我是如何離開的 – Liam

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

構造函數完全忽略了它的三個參數。它不會將它們分配給相應的字段,所以這些字段保持其默認值:null。

0

看看構造函數的人和學生,應該使用方法頭中的參數。

super(name,dob,address)