-1

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

MikaelF
  • 3,518
  • 4
  • 20
  • 33
Chris
  • 1
  • 1
  • I did not show it but I have imported the necessary packages already: import java.util.*; import java.io.File; – Chris Feb 23 '17 at 01:37
  • Could we view your content in the notepad ? – FreedomPride Feb 23 '17 at 01:38
  • 2
    You dont `payrollType` inititalized anywhere. And it is always `null` in your code – Md Johirul Islam Feb 23 '17 at 01:38
  • 1
    `payrollType` is null because you don't set it until inside the if statement. You can't use `.equals` on a null variable. – D M Feb 23 '17 at 01:39
  • The heuristic for debugging a NullPointerException is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. In the future, please search on the subject before posting, since this is too common a problem to post yet another NPE question. – Hovercraft Full Of Eels Feb 23 '17 at 01:40
  • FreedomPride- It prints out: Employee ID Name Hours Worked Pay Amount and then java.lang.NullPointerException at PayrollPreparation.printEmployeeData(PayrollPreparation.java:71) – Chris Feb 23 '17 at 01:40
  • Is payrollType not initialized in the global attributes where I have private String payrollType; ?? – Chris Feb 23 '17 at 01:47

1 Answers1

0

Your property payrollType is never initialized.

When calling the equals method on a null object, this result as NullPointerException.

The best practice is always compare a constant (in your case the letter S ) with an object.

So your code should be "S".equals (payrollType).