0

完全なエラーはこちら

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 のサブクラスです。これら2つのコードは次のとおりです。

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();
}

誰かが私を正しい方向に向けることができれば、本当に感謝しています。ありがとうございました!

4

1 に答える 1

2

地方宣言

Employee[] employees = new Employee[employeeMax];

インスタンス変数を非表示にします。次を試してください。

employees = new Employee[employeeMax];

それ以外のemployees場合、インスタンス化するのは、コンストラクターの呼び出し後に消えるローカル変数です。

あなたの状況では、

Employees[] e = NULL;

method()
{
   Employees[] e = new Employees[N]; // this declares a different variable with same name
}

method2()
{
  .. e[0] .. // exception: e was still NULL
}
于 2012-09-19T02:54:26.220 に答える