这是完整的错误
Exception in thread "main" java.lang.NullPointerException
at EmployeeManager.addEmployee(EmployeeManager.java:38)
at EmployeeDriver.main(EmployeeDriver.java:231)
这是错误的来源
public class EmployeeManager
{
private Employee[] employees;
private final int employeeMax = 100;
private int currentEmployees;
public EmployeeManager()
{
Employee[] employees = new Employee[employeeMax];
currentEmployees = 0;
}
public void addEmployee(int eType, String fn, String ln, char m, char g, int empNum, boolean ft, double a)
{
double sales = 0.0, hoursWorked = 0.0;
switch (eType)
{
/*error*/ case 1: employees[currentEmployees] = new HourlyEmployee(fn, ln, m, g, empNum, ft, a, hoursWorked);
currentEmployees++;
break;
case 2: employees[currentEmployees] = new SalaryEmployee(fn, ln, m, g, empNum, ft, a);
currentEmployees++;
break;
case 3: employees[currentEmployees] = new CommissionEmployee(fn, ln, m, g, empNum, ft, a, sales);
currentEmployees++;
break;
}
}
///////////////////////////////////////// //////////////////////////////////
//Add Employee
case 2:
String fn, ln;
char mi, g, f;
boolean ft = true;
do
{
System.out.println("\n1. Hourly");
System.out.println("2. Salary");
System.out.println("3. Commission");
System.out.print("Enter Choice: ");
subInput1 = in.nextInt();
if(subInput1 < 1 || subInput1 > 3)
{
System.out.println("Invalid Choice! Choose again.");
}
}while(subInput1 < 1 || subInput1 > 3);
System.out.print("Enter Last Name: ");
ln = in.next();
System.out.print("Enter First Name: ");
fn = in.next();
System.out.print("Enter Middle Initial: ");
mi = in.next().charAt(0);
System.out.print("Enter Gender: ");
g = in.next().charAt(0);
System.out.print("Enter Employee Number: ");
en = in.nextInt();
System.out.print("Full Time? (y/n): ");
f = in.next().charAt(0);
if(f == 'n' || f == 'N')
{
ft = false;
}
if(subInput1 == 1)
{
System.out.print("Enter wage: ");
}
else if(subInput1 == 2)
{
System.out.print("Enter salary: ");
}
else
{
System.out.print("Enter rate: ");
}
amount = in.nextDouble();
/*error*/ em.addEmployee(subInput1, fn, ln , mi, g, en, ft, amount);
em.removeRedundancies();
break;
现在就我而言,我正在创建一个 HourlyEmployee 的新实例,它将所有这些值保存在员工数组中。HourlyEmployee 是超类 Employee 的子类。这是我的这两个代码。
public class HourlyEmployee extends Employee
{
private double wage;
private double hoursWorked;
public HourlyEmployee(String fn, String ln, char m, char g, int empNum, boolean ft, double w, double h)
{
super (fn, ln, m, g, empNum, ft);
wage = w;
hoursWorked = h;
hoursWorked = 0.0;
}
@Override
public String toString()
{
return this.getEmployeeNumber() + "\n" + lastName + ", " + firstName + middleInitial + "\n" + "Gender: "
+ this.getGender() + "\n" + "Status: " + fulltime + "\n" + "Wage: " + wage + "\n" + "Hours Worked: " + hoursWorked + "\n";
}
@Override
public double calculateWeeklyPay()
{
if (hoursWorked > 40)
{
wage = wage * 2;
}
return wage * hoursWorked;
}
@Override
public void annualRaise()
{
wage = (wage * .05) + wage;
}
@Override
public double holidayBonus()
{
return wage * 40;
}
@Override
public void resetWeek()
{
hoursWorked = 0.0;
}
public double plusHoursWorked(double amount, double h)
{
while (amount <= 0)
{
System.out.println("Invalid value entered, please try again");
}
return amount + h;
}
}
///////////////////////////////////////// //////////////////////////////////
public abstract class Employee
{
protected String firstName;
protected String lastName;
protected char middleInitial;
protected boolean fulltime;
private char gender;
private int employeeNum;
public Employee (String fn, String ln, char m, char g, int empNum, boolean ft)
{
firstName = fn;
lastName = ln;
middleInitial = m;
gender = g;
employeeNum = empNum;
fulltime = ft;
}
public int getEmployeeNumber()
{
return employeeNum;
}
public void setEmployeeNumber(int empNum)
{
while (empNum <= 10000 && empNum >= 99999)
{
System.out.print ("Invalid input, please try again: ");
}
if (empNum >= 10000 && empNum <= 99999)
{
employeeNum = empNum;
}
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public char checkGender(char g)
{
if (g != 'M' || g != 'F')
{
g = 'F';
}
return g;
}
public char getGender()
{
return gender;
}
@Override
public boolean equals(Object e2)
{
if (this.employeeNum == ((Employee)e2).employeeNum)
{
return true;
}
else
{
return false;
}
}
@Override
public String toString()
{
return employeeNum + "\n" + lastName + ", " + firstName + "\n" + "Gender:" + gender + "\n" + "Status:" + fulltime + "\n";
}
public abstract double calculateWeeklyPay();
public abstract void annualRaise();
public abstract double holidayBonus();
public abstract void resetWeek();
}
老实说,我不知道我做错了什么,如果有人能指出我正确的方向,我将不胜感激。谢谢!