打印对象数组时出现NullPointerException

时间:2014-02-09 17:13:55

标签: java

我有这个实验室要上大学,我得到java.lang.NullPointerException我无法弄清楚代码的问题。

package Lab2Revision;

public class Manager extends Employee {

    private String deptName;
    private Employee[] staff;
    private int employeeCount = 0;

    public Manager(int empid, String name, String ssn, double salary, String deptName)
    {
        super(empid, name, ssn, salary);
        this.deptName = deptName;
        staff = new Employee[20];
    }

    public String getDeptName(){
        return deptName;
    }

    public int findEmployee(Employee e)
    {
        int a =0;
        for(int i = 0; i < staff.length; i++)
        {
            if(e == staff[i])
            {
                a = -1;
            }
            else
            {
                a = i;
            }
        }
        return a;
    }

    public boolean addEmployee(Employee e)
    {
        boolean a = true;

            if(findEmployee(e) != -1)
            {
                staff[employeeCount] = e;
                employeeCount++;
                a = true;
                System.out.println(staff[employeeCount]);
            }
            else
            {
                a = false;
            }

        return a;
    }

    public boolean removeEmployee(Employee e)
    {
        boolean a = false;
        Employee [] tempStaff = null;
        int tempCount = 0;

        for(int i = 0; i < staff.length; i++)
        {
            if(staff[i].getEmpid() != e.getEmpid())
            {
                tempStaff[tempCount] = staff[i];
                tempCount++;
            }
            else
            {
                a = true;
            }
        }
        staff = tempStaff;
        employeeCount = tempCount;
        return a;
    }

    public String printStaffDetails()
    {
        String a = "Staff of " + getName() + "\n";

        for(int i = 0; i < staff.length; i++)
        {
        a += "Name: " + staff[i].getName() + " Employee id:  "  + staff[i].getEmpid() + "\n" ;
        }
        return a;
    }
    public String toString(){
        return super.toString() + "Department: \t " + deptName + "\n";
    }
}

这是我的考验:

package Lab2Revision;

public class TestEmployee {

    public static void main(String [] aargs){

        Engineer e1 = new Engineer(101, "Jane Smith", "012-34-5678", 120345.27,"");
        Admin  e2 = new Admin(304, "Bill Munroe", "108-23-6509", 75002.34,"");
        Manager e3 = new Manager(207, "Barbara Johnson",  "054-12-2367", 109501.36, "US Marketing");
        Director  e4 = new Director(12, "Susan Wheeler", "099-45-2340", 120567.36, "Global Marketing", 1000000.0);
        Engineer e5 = new Engineer(120, "Bill Lecomte", "045-89-1010", 110450.34,"");

        System.out.println(e1);
        System.out.println(e5);
        System.out.println(e2);
        System.out.println(e3);
        System.out.println(e4);

        e3.addEmployee(e1);
        e3.addEmployee(e2);
        e3.addEmployee(e5);

        /*if(e3.addEmployee(e1) == true)
        {
            System.out.println("Success: added admin");
        }

        if(e3.addEmployee(e2) == true)
        {
            System.out.println("Success: added eng1");
        }

        if(e3.addEmployee(e5) == true)
        {
            System.out.println("Success: added eng2");
        }
        */

        e3.raiseSalary(10000);

        System.out.println(e3.printStaffDetails());;  
    }
}

问题在于addEmployee()方法,我只想解决这个问题,如果你看到这个错误不会产生的其他问题,(除非它只是更好的编码实践)请不要告诉我,我会自己来看看它。

staff[employeeCount] = e;方法中的行addEmployee()似乎没有将Employee添加到staff数组中,从而为我提供空值并导致错误。

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

哎呀,我显然在没有仔细查看代码的情况下发布了答案。

addEmployee()有效(排序,还有问题)

staff []数组初始化为20,但在您的测试方法中,您只添加了3名员工。 printStaffDetails()中的循环将循环遍历所有20个元素,其中只有3个元素为非null。所以调用staff [i] .getName()将抛出nullPointer。

答案 1 :(得分:0)

实例变量staff初始化为长度20.您在printStaffDetails方法中从0到19进行迭代,无论实际添加了多少员工。您应该使用employeeCount作为循环的上限。

正如宗正指出的那样,如果你在删除一些员工之后添加员工,这将再次破裂。明显的解决方法是循环所有这些并检查null - ness,或使用像List这样的理智数据结构。