I'm getting an error in my Java code but I can't see whats wrong with it. Help?

Posted by Fraz on Stack Overflow See other posts from Stack Overflow or by Fraz
Published on 2010-03-25T15:34:50Z Indexed on 2010/03/25 15:43 UTC
Read the original article Hit count: 379

Filed under:
|
|
|

The error i'm getting is in the fillPayroll() method in the while loop where it says payroll.add(employee). The error says I can't invoke add() on an array type Person but the Employee class inherits from Person so I thought this would be possible. Can anyone clarify this for me?

import java.io.*;
import java.util.*;

public class Payroll
{   
    private int monthlyPay, tax;
    private Person [] payroll = new Person [1];

        //Method adds person to payroll array
    public void add(Person person)
    {
        if(payroll[0] == null) //If array is empty, fill first element with person
        {
            payroll[payroll.length-1] = person;
        }
        else //Creates copy of payroll with new person added
        {
            Person [] newPayroll = new Person [payroll.length+1];
            for(int i = 0;i<payroll.length;i++)
            {
                newPayroll[i] = payroll[i];
            }
            newPayroll[newPayroll.length] = person;
            payroll = newPayroll;
        }
    }


    public void fillPayroll()
    {
        try
        {
            FileReader fromEmployee = new FileReader ("EmployeeData.txt");
            Scanner data = new Scanner(fromEmployee);
                        Employee employee = new Employee();

            while (data.hasNextLine())
            {
                employee.readData(data.nextLine());
                payroll.add(employee);
            }

        }
        catch (FileNotFoundException e)
        {
            System.out.println("Error: File Not Found");
        }

    }

}

© Stack Overflow or respective owner

Related posts about java

Related posts about error