0

So I have three classes, Employee, Event, and Main. The purpose of my program is to create a event management system where the user enters the name of the event along with the names and ID's of employees attending that event. I have an employee object, and an event object. The attributes of my employee object are String name and int ID. The attributes of my event object are name and an ArrayList of Employee objects that are attending that event. Now the problem is that in my main method, when I am asking the user to enter the name of the event, the number of employees in the event, then proceeding to ask the names and ID's of the employees attending the event, the problem is that after the information from the user is obtained, when I try to display the Employees (their names for example) that are attending an event, it just returns a null arraylist.

The problem I am having is from the line at the end of the code where is prints out the attendees for a particular event which I have identified with asterisk.

Here is some code from the main class:

public class Main {

    public static void main (String[] args) {

        ArrayList<Event> events = new ArrayList<Event>();
        ArrayList<Employee> employees = new ArrayList<Employee>();

        Event event = null;
        Employee employee = null;       

        Scanner key = new Scanner (System.in);      
        String name, employeeName, id = "";
        int numOfEmployees, attendees = 0;

        System.out.print("How many events? "); 
        numOfEmployees = key.nextInt(); key.nextLine();
        for (int i = 0; i < numOfEmployees; i++) {
            System.out.println("===========================================");
            System.out.print("Enter the name of the event: "); 
            name = key.nextLine();          

            System.out.print("How many attendees? "); 
            attendees = key.nextInt(); key.nextLine();  

            for (int k = 0; k < attendees; k++) {       
                System.out.println("----------------------------------------");
                System.out.print("Enter the employee name: "); 
                employeeName = key.nextLine();

                System.out.print("Enter the employee IDs of the attendees: "); id = key.nextLine();

                int temp = Integer.parseInt(id);
                employee = new Employee(employeeName, temp);

                System.out.println(employee);
                employees.add(employee);

            }       

            System.out.println(employees);
            event = new Event(name, employees); 

            events.add(event);          
            //event = null; 
            employees.clear();          
        }

        System.out.println(events.get(1).getName());
        **System.out.println(events.get(1).getAttendees());**

        key.close();            
    }
}

If you want the code from my event class and employee class I can give that to you too, but here is just the constructors for both:

Constructor for event class:

public Event (String name, ArrayList<Employee> employees) {
        this.name = name;
        this.events.add(name);
        this.attendees = employees;
    }

Constructor for employee class:

public Employee(String name, int ID) {
        this.name = name;
        this.ID = ID;
        this.employees.add(ID);
    }
Gjison Giocf
  • 145
  • 1
  • 11

1 Answers1

0

Try reading this: Is Java "pass-by-reference" or "pass-by-value"?

Creating a new ArrayList and assigning it to e solves the problem.

Replacing

e.clear();   

with

 e = new ArrayList<Employee>();

should solve it. (Or you can also use a clone in the Event constructor.)

All events point to the same ArrayList, so

e.clear()

clears attendees across all events.

Barath Sankar
  • 383
  • 4
  • 15