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();
}
}
}
}
getFileis returning a null.getResource(String)that's returningnull, but the rest of your advice is pretty sound.