This line of code gave me an error.
Scanner sce = new Scanner(new File("employees.txt"));
while(sce.hasNextLine()) {
String[] obj = sce.nextLine().split(", ");
Employee e = new Employee(obj);
department d = new department(e); //Here is where the error pops up (Main.java:29)
...
}
I'm scanning the text file and splitting the different fields into a string array then declaring an object from the Employee
class. I then declare an object from the department
class using the object from the Employee
class and I run into this error:
Exception in thread "main" java.lang.NullPointerException
at ssst.edu.ba.department.<init>(department.java:19)
at ssst.edu.ba.Main.main(Main.java:29)
My department
Class:
package ssst.edu.ba;
import java.util.ArrayList;
public class department {
private ArrayList<Employee> Marketing;
private ArrayList<Employee> Production;
private String department;
public department() {
}
public department(String department) {
this.department = department;
}
public department(Employee employee) {
if(employee.getDepartment() == "Marketing") Marketing.add(employee);
else Production.add(employee); //This is the other location (department.java:19)
}
public String getDepartment() {
return department;
}
public ArrayList<Employee> getMarketing() {
return Marketing;
}
public ArrayList<Employee> getProduction() {
return Production;
}
}
The file employees.txt is as follows:
Ginny, Gullatt, Marketing, 1000 Tiara, Curd, Production, 1200 amie, Poorman, Marketing, 900 Jammie, Hasson, Marketing, 800 Lionel, Hailey, Marketing, 500 Genevive, Mckell, Production, 2000 Esteban, Slaubaugh, Marketing, 1300 Elden, Harte, Production, 1340 Tasia, Rodrigue, Marketing, 1200 Nathanial, Dentler, Production, 1700 Valda, Nicoletti, Marketing, 600 Kary, Wilkerson, Production, 600 Coletta, Akey, Marketing, 800 Wilmer, Jack, Production, 600 Loreta, Agnew, Marketing, 700 Suzy, Cleveland, Production, 1450 Pasty, Laprade, Marketing, 1300 Candie, Mehaffey, Production, 1800 Glady, Landman, Marketing, 1900 Tierra, Mckeown, Production, 2200