-1
我想從txt文件返回某些有關基於工資的員工的變量,代碼將編譯但不能打印出來。 這是我目前已經在我的項目完成:幫助空指針異常
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.");
}
}
代碼中所有編譯但測試時沒有與if
聲明一個錯誤,它說java.lang.NullPointerException: null
我沒有顯示它,但我已經導入了必要的包:import java.util。*; import java.io.File; – Chris
我們可以在記事本中查看您的內容嗎? – FreedomPride
你不''任何地方'payrollType'初始化。在代碼 –