I am working on a program for class due today at midnight and I just can’t figure why it’s giving me this Null Point Exception error. I would really appreciate if you guys can look at my code and help me out.
The goal of the project is as follows..
-
Program Statement: Write a Payroll class that uses the following arrays as fields:
- employeeId – an array of 7 integers to hold employee id numbers. The array field should be initialized with the following numbers:
- 5658845
- 4520125
- 7895122
- 8777541
- 8451277
- 1302850
- 7580489
- hours – An array of seven integers to hold the number of hours worked by each employee
- payRate – An array of seven doubles to hold each employee’s hourly rate.
- wages – An array of seven doubles to hold each employee’s gross wages.
The class should relate the data in each array through the subscripts. For example, the number in element 0 of the hours array should be the number of hours worked by the employee whose ID number is stored in Element 0 of the employeeId array. That same employees pay rate should be stored in Element 0 of the payRate array. In addition to the appropriate accessor and mutator methods, the class should have a method that accepts an employee’s id number as an argument and returns the gross pay for that employee. Demonstrate the class in a complete program that displays each employee number and asks the user to enter the employee’s hours and pay rate. It should then display each employee’s id number and gross wages.
- Input Validation : Do not accept negative values for numbers and numbers less than 6.00 for pay rate.
- employeeId – an array of 7 integers to hold employee id numbers. The array field should be initialized with the following numbers:
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);
}
It looks like you are not initializing the
hours,payRateorwagesvariables, which means they are all null. When you call, say,hours[i] = kboard.nextInt()it will throw a NullPointerException because you can’t set the i th element ofnull.Make sure you have a line like
hours = new int[whatever]before you try to use thehoursvariable (same for the other two). You’ve got it right foremployeeId, so follow that pattern.