На данный вопрос уже ответили:
У меня возникает ошибка:
Exception in thread "main" java.lang.NullPointerException
at Department.getWorkerWithMaxSalary(Department.java:19)
at Table.main(Table.java:42)
вот мой код Department:
import java.util.*;
public class Department implements Comparable<Department>{
private String nameOfTheDepartment;
private Worker directorOfTheDepartment;
private Set<Worker> workerList;
private Worker chairman;
private Set<Department> listOfDepartments;
public Department(String nameOfTheDepartment, Worker directorOfTheDepartment, Set<Worker> setOfDepartament) {
this.nameOfTheDepartment = nameOfTheDepartment;
this.directorOfTheDepartment = directorOfTheDepartment;
this.workerList = workerList;
}
public Worker getWorkerWithMaxSalary(){
Worker workerWithMaxSalary = directorOfTheDepartment;
**Department.java:19** for (Worker worker : workerList){
if(workerWithMaxSalary.getSalary()<worker.getSalary())
workerWithMaxSalary = worker;
}
return workerWithMaxSalary;
}
public Set<Worker> getFullWorkerList(){
Set<Worker> workerList = new TreeSet<Worker>(new SalaryComparator());
workerList.add(this.chairman);
Iterator iter = listOfDepartments.iterator();
while (iter.hasNext()){
Department department = (Department)iter.next();
workerList.add(department.getDirectorOfTheDepartment());
workerList.addAll(department.getWorkerList());
}
return workerList;
}
@Override
public int compareTo(Department b) {
int result = this.nameOfTheDepartment.compareTo(b.nameOfTheDepartment);
if (result != 0) {
return result;
}
return this.nameOfTheDepartment.compareTo(b.nameOfTheDepartment);
}
public void addWorker(Worker worker){
workerList.add(worker);
}
public String getNameOfTheDepartment() {
return nameOfTheDepartment;
}
public void setNameOfTheDepartment(String nameOfTheDepartment) {
this.nameOfTheDepartment = nameOfTheDepartment;
}
public Worker getDirectorOfTheDepartment() {
return directorOfTheDepartment;
}
public void setDirectorOfTheDepartment(Worker directorOfTheDepartment) {
this.directorOfTheDepartment = directorOfTheDepartment;
}
public Set<Worker> getWorkerList() {
return workerList;
}
public void setWorkerList(Set<Worker> workerList) {
this.workerList = workerList;
}
}
Вот код Firm:
import java.util.*;
public class Firm {
private String nameOfFirm;
private Worker chairman;
private Set<Department> listOfDepartments;
public Firm(String nameOfFirm, Worker chairman) {
this.nameOfFirm = nameOfFirm;
this.chairman = chairman;
this.listOfDepartments = listOfDepartments;
}
public Set<Department> findAllDepartmentsWihLowDirectorSalary(){
Set<Department> departmentList = listOfDepartments;
Iterator<Department> iter = listOfDepartments.iterator();
while (iter.hasNext()){
Department department = iter.next();
if(department.getDirectorOfTheDepartment()!=department.getWorkerWithMaxSalary())
departmentList.add(department);
}
return departmentList;
}
public Worker getWorkerWithMaxSalary(){
Worker workerOfTheFirmWithMaxSalary = chairman;
for (Department department : listOfDepartments){
if(workerOfTheFirmWithMaxSalary.getSalary() < department.getWorkerWithMaxSalary().getSalary())
workerOfTheFirmWithMaxSalary = department.getWorkerWithMaxSalary();
}
return workerOfTheFirmWithMaxSalary;
}
public void addDepartment(Department department) {
listOfDepartments.add(department);
}
public String getNameOfFirm() {
return nameOfFirm;
}
public void setNameOfFirm(String nameOfFirm) {
this.nameOfFirm = nameOfFirm;
}
public Worker getChairman() {
return chairman;
}
public void setChairman(Worker chairman) {
this.chairman = chairman;
}
public Set<Department> getListOfDepartments() {
return listOfDepartments;
}
public void setListOfDepartments(Set<Department> listOfDepartments) {
this.listOfDepartments = listOfDepartments;
}
}
Вот Компаратор:
import java.util.Comparator;
public class SalaryComparator implements Comparator<Worker> {
SalaryComparator() { }
public int compare(Worker w1, Worker w2) {
int result = w1.getWorkerName().compareTo(w2.getWorkerName());
return result != 0 ? result : (int)(w1.getSalary() - w2.getSalary());
}
}
Вот код Worker:
public class Worker {
private String workerName;
private String workerSurname;
private int salary;
public Worker(String workerName, String workerSurname, int salary) {
this.workerName = workerName;
this.workerSurname = workerSurname;
this.salary = salary;
}
@Override
public String toString() {
return "[Name=" + workerName + ", Surname=" + workerSurname + ", salary=" + salary + "]";
}
public String getWorkerName() {
return workerName;
}
public void setWorkerName(String workerName) {
this.workerName = workerName;
}
public String getWorkerSurname() {
return workerSurname;
}
public void setWorkerSurname(String workerSurname) {
this.workerSurname = workerSurname;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
Вот класс, где создаются списки и прочее:
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class Table {
public static void main(String[] args){
Worker chairman = new Worker("Jordj", "Valit", 3000);
//DevDepartment
Worker devDepartmentDirector = new Worker("Joe", "Hanh", 13000);
Worker worker1 = new Worker("Mike", "Shinoda", 2500);
Worker worker2 = new Worker("Chester", "Bennington", 1500);
Worker worker3 = new Worker("Brad", "Delson", 1200);
//EngineringDepartment
Worker engineringDepartmentDirector = new Worker("Sharon", "del Adel", 600);
Worker worker4 = new Worker("Robert", "Westerholt", 600);
Worker worker5 = new Worker("Jeroen", "van Veen", 2000);
Worker worker6 = new Worker("Ruud", "Jolie", 1300);
Set<Worker> setf1 = new TreeSet<Worker>(new SalaryComparator());
setf1.add(worker1);
setf1.add(worker2);
setf1.add(worker3);
Set<Worker> setf2 = new TreeSet<Worker>(new SalaryComparator());
setf2.add(worker4);
setf2.add(worker5);
setf2.add(worker6);
Department f1 = new Department("DevDepartment", devDepartmentDirector, setf1);
Department f2 = new Department("EngineringDepartment", engineringDepartmentDirector, setf2);
Set<Department> setofDepartment = new TreeSet<Department>();
setofDepartment.add(f1);
setofDepartment.add(f2);
**Table.java:42** Worker workerWithMaxSalary = f1.getWorkerWithMaxSalary();
System.out.println("Максимальну зарплату має: "+workerWithMaxSalary.getWorkerName()+" "+workerWithMaxSalary.getWorkerSurname()+ " - "+ workerWithMaxSalary.getSalary() + " грн");
System.out.println(" ");
for(Department department: setofDepartment){
System.out.println("Директор DevDepartment : " + devDepartmentDirector);
System.out.println("Директор EngineringDepartment" + engineringDepartmentDirector);
System.out.println("Начальник : " + chairman);
System.out.println(" ");
System.out.println("Список усіх працівників компанії :");
for(Worker worker: department.getFullWorkerList()){
System.out.println(worker.getWorkerName()+" "+worker.getWorkerSurname());
}
}
}
}
Подскажите, пожалуйста, как решить эту проблему? Строки, где возникает ошибка обозначены. Буду очень благодарна.
Заменить setOfDepartament
на workerList
(или добавить) ? у вас workerList
не инициализирован
public Department(String nameOfTheDepartment, Worker directorOfTheDepartment, Set<Worker> workerList) {
this.nameOfTheDepartment = nameOfTheDepartment;
this.directorOfTheDepartment = directorOfTheDepartment;
this.workerList = workerList;
}
так же у вас Department#chairman и Department#listOfDepartments не инициализированы тоже и будет NPE.
Виртуальный выделенный сервер (VDS) становится отличным выбором
Нужен совет по ООП
Установил Помощник обновления системных драйверов от интел, он говорит, что видео драйвер стоит последнийИгрушки на компе работают, появилась...