我正在制作今天午夜到期的课程,我无法理解为什么它会给我这个Null Point Exception错误。如果你们能看看我的代码并帮助我,我将非常感激。
该项目的目标如下。
程序声明:编写一个使用以下数组作为字段的Payroll类:
该类应通过下标关联每个数组中的数据。例如,hours数组的元素0中的数字应该是员工的工作小时数,该员工的ID号存储在employeeId数组的元素0中。同样的员工工资率应该存储在payRate数组的元素0中。除了适当的访问器和mutator方法之外,该类还应该有一个方法,该方法接受员工的id号作为参数,并返回该员工的总工资。在一个完整的程序中展示课程,该程序显示每个员工编号并要求用户输入员工的工时和工资率。然后它应显示每个员工的身份证号码和工资总额。
int[] employeeId;
int[] hours;
double[] payRate;
double[] wages;
Scanner kboard = new Scanner(System.in);
public ParrishPayroll(int[] ids){
employeeId = new int[ids.length];
// Copy the values in ids
for (int index = 0; index < ids.length; index++)
employeeId[index] = ids[index];
System.out.println("Employee ID's");
System.out.println(Arrays.toString(employeeId));
System.out.println("Please enter the id number you would like to edit.");
int input = kboard.nextInt();
if(input == 5658845){
int index = 0;
setHours(index);
}
else if(input == 4520125){
int index = 1;
setHours(index);
}
else if(input == 7895122){
int index = 2;
setHours(index);
}
else if(input == 8777541){
int index = 3;
setHours(index);
}
else if(input == 8451277){
int index = 4;
setHours(index);
}
else if(input == 1302850){
int index = 5;
setHours(index);
}
else if(input == 7580489){
int index = 6;
setHours(index);
}
else {
System.out.println("Invalid ID number!");
}
}//end startSequence
public void setHours(int i){
System.out.println("How many hours were worked?");
hours[i] = kboard.nextInt();
if(hours[i] < 0){
System.out.println("Please input a positive number.");
kboard.nextInt(hours[i]);
setPayRate(i);
}
else
setPayRate(i);
}
public void setPayRate(int index){
int input = index;
System.out.println("What is the employee's pay rate?");
payRate[input] = kboard.nextDouble();
if(payRate[input] < 0){
System.out.println("Please input a positive number.");
payRate[input] = kboard.nextDouble();
calcWages(input);
}
else if(payRate[index] < 6.00){
System.out.println("Wages must be higher than $6.00.");
payRate[input] = kboard.nextDouble();
calcWages(input);
}
}
public void calcWages(int index){
int input = index;
wages[input] = hours[input] * payRate[input];
}
public void getGross(int i){
int input = i;
if(input == 5658845){
System.out.print("Employee number: " + input + " wages: " + wages[0]);
}
else if(input == 4520125){
System.out.print("Employee number: " + input + " wages: " + wages[1]);
}
else if(input == 7895122){
System.out.print("Employee number: " + input + " wages: " + wages[2]);
}
else if(input == 8777541){
System.out.print("Employee number: " + input + " wages: " + wages[3]);
}
else if(input == 8451277){
System.out.print("Employee number: " + input + " wages: " + wages[4]);
}
else if(input == 1302850){
System.out.print("Employee number: " + input + " wages: " + wages[5]);
}
else if(input == 7580489){
System.out.print("Employee number: " + input + " wages: " + wages[6]);
}
else {
System.out.println("Invalid ID number!");
getGross(input);
}
}
public static void main(String[] args){
int[] idlist = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
ParrishPayroll user1 = new ParrishPayroll(idlist);
Scanner kboard = new Scanner(System.in);
System.out.println("What employee would you like to see the gross wages for?");
int i = kboard.nextInt();
user1.getGross(i);
}
答案 0 :(得分:0)
您似乎没有初始化hours
,payRate
或wages
变量,这意味着它们都是空的。当您调用hours[i] = kboard.nextInt()
时,它将抛出NullPointerException,因为您无法设置null
的 i 元素。
在尝试使用hours = new int[whatever]
变量之前,请确保您有hours
之类的行(其他两个变量相同)。你已经适合employeeId
,所以请遵循这种模式。