简体   繁体   English

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

[英]Java: Adding an object to an Array List and retrieving it prints out null

I have two classes (actually more, but that's not relevant).我有两个班级(实际上更多,但这无关紧要)。 In my main class I have a method called createEmployee() , in which I create an Employee with user input.在我的类中,我有一个名为createEmployee()的方法,在该方法中我使用用户输入创建了一个 Employee 。 Here's the Main class:这是主类:

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

}

In my ReusaxCorp class I have a method registerEmployee , which should add a new Employee to the Array List and retrieveEmployee , which should print all registered Employees.在我的ReusaxCorp类中,我有一个方法registerEmployee ,它应该向数组列表和retrieveEmployee添加一个新员工,它应该打印所有注册的员工。 Here they are:他们来了:

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

The problem I have is that it always prints out ID: null Name: null salary: 0.0 and I don't know what that is.我遇到的问题是它总是打印出 ID: null Name: null salary: 0.0我不知道那是什么。 I also tried doing - for instance - this:我也尝试过 - 例如 - 这个:

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

In my retrieveEmployee() method I then wrote getEmpID() instead of ID , but it still printed out null.在我的retrieveEmployee() 方法中,我写了getEmpID()而不是ID ,但它仍然打印出null。 Where's the mistake?错在哪里?

You are in effect, creating a new Employee object, doing nothing with it, and then calling a method in your ReusaxCorp that creates a new Employee object with no values and adds it to your "employees" list.实际上,您创建了一个新的 Employee 对象,不对其进行任何操作,然后调用 ReusaxCorp 中的一个方法,该方法创建一个没有值的新 Employee 对象并将其添加到您的“员工”列表中。 When you call your "retrieveEmployees" method, it prints out your empty employee.当您调用“retrieveEmployees”方法时,它会打印出您的空员工。

Solve this by:解决这个问题:

in your "createEmployee" class, make the following change in your "switch" statement:在“createEmployee”类中,在“switch”语句中进行以下更改:

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

and in your "ReusaxCorp" class, change your "registerEmployee" method:并在您的“ReusaxCorp”类中,更改您的“registerEmployee”方法:

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

Ultimately, the problem is that you never used any of the objects you created, and just populated the list with "empty" Employee objects.最终,问题在于您从未使用过您创建的任何对象,而只是使用“空”Employee 对象填充列表。

Hope this helped.希望这有帮助。

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

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