I am trying to return certain variables about the salary based employees from a txt file and the code will compile but not print out. This is what I currently have done in my project:
public class PayrollPreparation {
private File source;
private Scanner input;
private String payrollType;
private int employeeID;
private String employeeName;
private int hoursWorked;
private double payRate;
private double straightPay;
public void printEmployeeData() {
System.out.println("Employee ID Name Hours Worked Pay Amount");
try {
source = new File("Payroll.txt");
input = new Scanner(source);
input.useDelimiter("[,\n]");
} catch (Exception e) {
System.out.println(e);
}
while (input.hasNext()) {
if (payrollType.equals("S")) {
employeeID = input.nextInt();
employeeName = input.next();
hoursWorked = input.nextInt();
payRate = input.nextDouble();
payrollType = input.next();
straightPay = (hoursWorked * payRate);
System.out.println(" " + employeeID + " " + employeeName
+ " "
+ hoursWorked + " " + straightPay);
input.nextLine();
}
}
System.out.println("No More Salaried Employees.");
}
}
The code all compiles but when tested there is an error with the if
statement and it says java.lang.NullPointerException: null