简体   繁体   中英

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. 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. 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. 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. 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. When you call your "retrieveEmployees" method, it prints out your empty employee.

Solve this by:

in your "createEmployee" class, make the following change in your "switch" statement:

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

and in your "ReusaxCorp" class, change your "registerEmployee" method:

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.

Hope this helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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