0

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
jhamon
  • 3,603
  • 4
  • 26
  • 37
Noomy
  • 17
  • 4
  • You never initialize `Production` or `Marketing` eg `Marketing = new ArrayList<>();` – Mark Dec 06 '19 at 15:52
  • 3
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – jhamon Dec 06 '19 at 15:53

2 Answers2

1

I would say you just forgot to instantiate your lists

private ArrayList<Employee> marketing;

And in the constructor

marketing = new ArrayList<Employee>();

An other point, variable names should start with lowercase. Uppercase are for classname.

Minirock
  • 628
  • 3
  • 13
  • 28
  • since we have 3 constructors, that is not the best solution, but correct (if extended to the other constructors) – user85421 Dec 06 '19 at 16:11
0

The arraylist Production is uninitialized as well as Marketing. In the constructor you should initialize all the class variables. Also, compare Strings with .equals()

public department(Employee employee) { 
    Marketing = new ArrayList<Employee>();
    Production = new ArrayList<Employee>();
    if("Marketing".equals(employee.getDepartment())) Marketing.add(employee);
    else Production.add(employee); 
}

EDIT: Doing "Marketing".equals() prevents a null pointer exception as Christopher pointed out

Tyler
  • 955
  • 9
  • 20
  • 1
    In most cases, it's even better to reverse that string comparison logic. `"Marketing".equals(employee.getDepartment());`, though sometimes that can hide a legitimate error. This prevents a NullPointerException if `getDepartment` returns null. If it's required that `department` is never null, you shouldn't allow it to be null. – Christopher Schneider Dec 06 '19 at 15:58
  • Then we disagree. I find that is easier to read and is certainly less verbose than `employee.getDepartment() != null && employee.getDepartment().equals("Marketing")`. It is a perfectly legitimate use-case, and the logic is identical. – Christopher Schneider Dec 06 '19 at 16:30