In my task I have 2 classes Kadry
(personnel) and Pracownik
(worker).
Pracownik
looks like this (with getters and setters):
public class Pracownik {
private String name;
private String lastName;
private double salary;
private char gender;
private int structure;
public Pracownik( String name, String lastName, double salary, char gender, int structure) {
this.name = name;
this.lastName = lastName;
this.salary = salary;
this.gender = gender;
this.structure = structure;
In the class Kadry there are fields private Pracownik[] workers
and private int employment
(holds actual number of workers), Constructor that initializes the fields. There is also a method that adds a worker to the list, and this point is where i got into troubles, because i keep getting NullPointException. My class Kadry looks like this:
public class Kadry {
private Pracownik[] workers_;
private int employement_;
public Kadry () {
Pracownik[] workers_ = new Pracownik[100];
employement_ = workers_.length;
}
public void addWorker(Pracownik p) {
for (int i = 0; i < 100; i++) {
if (workers_[i] == null) {
workers_[i] = p;
break;
}
else System.out.println("List is already full!!!");
}
}
public static void main(String[] args) {
Kadry k1 = new Kadry();
Pracownik Piotr = new Pracownik("Piotr", "Wanjas", 3000.00, 'M', 3);
k1.addWorker(Piotr);
System.out.println(k1.workers_[0].getName());
}
}
The NullPointException shows up at line if (workers_[i] == null) {