hey there, trying to do some college work although i keep getting a console error. i am tying to add three employees (an admin and 2 engineers) to the manager and then call printStaffDetails()
test class:
public class Test { public static void main(String[] args) { Engineer eng1 = new Engineer(101, "Jane Smith", "012-34-5678", 120567.36); Manager man = new Manager(207, "Barbara Johnson", "054-12-2367", 109501.36,"US Marketing"); Admin ad = new Admin(304, "Bill Munroe", "108-23-6509", 75002.34); Director dir = new Director(12, "Susan Wheeler", "099-45-2340", 120,"Global Marketing", 100000.0); Engineer eng2 = new Engineer(120, "Bill Lecomte", "045-89-1010", 110.450); System.out.println(eng1); System.out.println(man); System.out.println(ad); System.out.println(dir); System.out.println(eng2); man.raiseSalary(10000); man.setName("Barbara Johnson-Smythe"); System.out.println("CHANGE"); System.out.println(man); man.addEmployee(ad); man.addEmployee(eng1); man.addEmployee(eng2); man.printStaffDetails(); } }
Manager class:
import java.text.NumberFormat; public class Manager extends Employee { private String deptName; private Employee[] staff; private int employeeCount = 0; public Manager(int empid, String name, String ssn, double salary, String deptName) { super(empid, name, ssn, salary); this.deptName = deptName; staff = new Employee[20]; } public String getDeptName() { return deptName; } public String toString() { return " Employee ID: "+getId()+"\n Employee Name: "+getName()+"\n Employee SSN: "+getSsn() +"\n Employee Salary: €"+getSalary()+"\n Departmnt: "+deptName+"\n"; } public int findEmployee(String name) { int value = -1; for(int i = 0; i < staff.length; i++) { if(name.equals(staff[i].getName())) { value = i; } } return value; } public boolean addEmployee(Employee emp) { boolean b; int found = findEmployee(emp.getName()); if(found == -1) { staff[employeeCount] = emp; b = true; employeeCount ++; } else { b = false; } return b; } public boolean removeEmployee(Employee emp) { boolean b = false; Employee[] temp = new Employee[20]; int counter = 0; for(int i = 0; i < staff.length; i++) { if(emp.getId() != staff[i].getId()) { temp[i] = staff[i]; counter++; } else if(emp.getId() == staff[i].getId()) { b = true; staff = temp; employeeCount = counter; } } return b; } public void printStaffDetails() { System.out.println("Staff of "+getName()+": \n" ); for(int i = 0; i < staff.length; i++) { System.out.println("Name: "+staff[i].getName()+"\t id: "+staff[i].getId()); } } }
error in console:
Employee ID: 101 Employee Name: Jane Smith Employee SSN: 012-34-5678 Employee Salary: €120567.36 Employee ID: 207 Employee Name: Barbara Johnson Employee SSN: 054-12-2367 Employee Salary: €109501.36 Departmnt: US Marketing Employee ID: 304 Employee Name: Bill Munroe Employee SSN: 108-23-6509 Employee Salary: €75002.34 Employee ID: 12 Employee Name: Susan Wheeler Employee SSN: 099-45-2340 Employee Salary: €120.0 Department: Global Marketing Budget: €100000.0 Employee ID: 120 Employee Name: Bill Lecomte Employee SSN: 045-89-1010 Employee Salary: €110.45 CHANGE Employee ID: 207 Employee Name: Barbara Johnson-Smythe Employee SSN: 054-12-2367 Employee Salary: €119501.36 Departmnt: US Marketing Exception in thread "main" java.lang.NullPointerException at Manager.findEmployee(Manager.java:33) at Manager.addEmployee(Manager.java:44) at Test.main(Test.java:26)
cheers