Java:将对象添加到数组列表并检索它会打印出null

时间:2017-10-09 01:09:33

标签: java methods

我有两个班级(实际上更多,但这不相关)。在我的 main 类中,我有一个名为createEmployee()的方法,在其中我创建了一个带有用户输入的Employee。 这是Main类:

import java.util.*;

public class Main {

private static String END_LINE;
private Scanner sc;
public String name;
public String ID;
public double salary;
private ReusaxCorp reusaxcorp;

public Main(){
    sc = new Scanner(System.in);
    END_LINE = System.lineSeparator();
}

public void presentoptions(){
    reusaxcorp = new ReusaxCorp();
    while (true){
        System.out.println("=== Welcome === ");
        System.out.println("Choose an option below: ");
        System.out.println(" ");
        System.out.println("1. Register an employee. ");
        System.out.println("2. Remove an employee. ");
        System.out.println("3. Retrieve all employees information. ");
        System.out.println("4. Quit this program. ");
        int option = sc.nextInt();

        switch (option) {
            case 1:
                System.out.println("What type of employee? " + END_LINE
                        + " - Intern. " + END_LINE
                        + " - Employee. " + END_LINE
                        + " - Manager. " + END_LINE
                        + " - Director." + END_LINE);
                String type = sc.nextLine();

                createEmployee();
                break;

            case 2:
                break;
            case 3:
                reusaxcorp.retrieveEmployee();
                break;
            case 4:
                System.out.println("You've quitted the program.");
                System.exit(0);

            default:
                System.out.println("Error. Please try again.");
                break;
        }
    }
}

String empID;
String empName;
double empSalary;

public void createEmployee(){
    String typeofemployee = sc.nextLine();

        System.out.println("What's the ID of the new " + typeofemployee + "?");
        String ID = sc.nextLine();
        System.out.println("Wheat's the name of the new " + typeofemployee + "?");
        name = sc.nextLine();
        System.out.println("What's the salary of the new " + typeofemployee + "?");
        salary = sc.nextDouble();

        Employee employee = new Employee(ID, name, salary);

        switch (typeofemployee) {

            case "Employee":
                reusaxcorp.registerEmployee();
                break;

            default:
                System.out.println("Error");
                break;
        }
}

public static void main(String[] args) {
    Main runcode = new Main();
    runcode.presentoptions();
}

}

在我的ReusaxCorp类中,我有一个方法registerEmployee,它应该将新的Employee添加到数组列表和retrieveEmployee,它应该打印所有已注册的Employees。他们在这里:

public class ReusaxCorp extends Main {

   ArrayList<Employee> employees = new ArrayList<Employee>();
   final String END_OF_LINE = System.lineSeparator();

   public void registerEmployee(){
       employees.add(new Employee(ID, name, salary));
   }

   public void retrieveEmployee() {
       Main main = new Main();
       for(Employee employee: employees){
           System.out.println("ID: " + ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "salary: " + employee.grossSalary);
   }
}

我遇到的问题是它总是打印出ID:null名称:null salary:0.0 我不知道那是什么。我也试过 - 例如 - 这个:

String empID;
String empName;
double empSalary;

// in my Main class I wrote getters and setters and this in my createEmployee method:

System.out.println("What's the ID of the new " + typeofemployee + "?");
String newID = sc.nextLine();
setEmpID(newID);

在我的retrieveEmployee()方法中,我写了getEmpID()而不是ID,但它仍然打印出null。哪里出错了?

1 个答案:

答案 0 :(得分:0)

您正在生效,创建一个新的Employee对象,不对其执行任何操作,然后在ReusaxCorp中调用一个方法,该方法创建一个没有值的新Employee对象,并将其添加到“employees”列表中。 当您调用“retrieveEmployees”方法时,它会打印出您的空员工。

解决这个问题:

在“createEmployee”类中,在“switch”语句中进行以下更改:

reusaxcorp.registerEmployee(employee); // Now you are actually passing your newly created Employee object

在您的“ReusaxCorp”课程中,更改“registerEmployee”方法:

registerEmployee(Employee employee){ // Here, you are receiving an Employee object
   employees.add(employee);          // and here you are adding it to your list.
}

最终,问题在于您从未使用过您创建的任何对象,只是使用“空”Employee对象填充列表。

希望这会有所帮助。