0

I am still working on this same program. I though I was almost done after a suggestion in another thread that I implement a try/catch statement which worked well, however I am now getting a "Exception in thread "main" java.lang.NullPointerException at SimpleJavaAssignment.Company.main(Company.java:31)"

Code that triggers the exception:

File file = new File(Company.class.getResource("input.txt").getFile());

Complete code:

package SimpleJavaAssignment;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class Company
{
  ArrayList<Department> deptList = new ArrayList<Department>();

  public Department checkDepartment(String name)
  {
    for(Department dept: deptList)
    {
      if(dept.getName().equals(name))
      {
      return dept;
      }
    }
    Department d = new Department(name);
    deptList.add(d);
    return d;
  }

  public static void main(String[] args)
  {

    System.out.println ("This program will compile and display the stored employee data.");

    Scanner inputFile = null;
    File file = new File(Company.class.getResource("input.txt").getFile());


    try {

        inputFile = new Scanner(file);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Company c = new Company();
    String input = inputFile.nextLine();



    while(inputFile.hasNextLine() && input.length() != 0)
    {

      String[] inputArray = input.split(" ");
      Department d = c.checkDepartment(inputArray[3]);
      d.newEmployee(Integer.parseInt(inputArray[2]), inputArray[0] + " " + inputArray[1], d);
      input = inputFile.nextLine();
    }

    System.out.printf("%-15s %-15s %-15s %-15s %n", "DEPARTMENT", "EMPLOYEE NAME", "EMPLOYEE AGE",
          "IS THE AGE A PRIME");
    for(Department dept:c.deptList)
    {
      ArrayList<Employee> empList = dept.getEmployees();
      for(Employee emp: empList)
      {
      emp.printInfo();
      }
    }
  }    
}
3
  • 1
    Whenever you get a NPE in a statement like that, break it up into smaller statements so you can see which operation if failing. Odds are that getFile is returning a null. Commented Jun 24, 2014 at 23:31
  • @HotLicks: It's getResource(String) that's returning null, but the rest of your advice is pretty sound. Commented Jun 24, 2014 at 23:32
  • getResource() is not meant for reading input files - its for reading resource files like .properties files, etc. You should simply read the file using the path. And yes, your input.txt needs to be in the classpath. If you are using maven and you put the file in resources folder your code will pick it up in this manner Commented Jun 24, 2014 at 23:32

1 Answer 1

4

When you invoke Company.class.getResource("input.txt")

You are using the relative resource name, which is treated relative to the class's package. So, are you sure there is a file called input.txt at the same level of package SimpleJavaAssignment?

You could alternatively just specify the absolute path of the file and pass that into the File constructor, as such:

File file = new File("/my/path/input.txt");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.