0

Sorry to post it so many times. I got stuck why the result is NULL. It's supposed to be true or false. I think the check value is not linked in the Employee Class. Please tell me which is the problem.

public class Employee {
    String name;
    PrimeAgeChecker checks;
    int age;
    Department department;
    public ArrayList<Employee> emplo;

    static Employee emp1 = new Employee(Department.Accounting,"Counting Guru",55);
    static Employee emp2 = new Employee(Department.Accounting,"Counting Pro", 45);
    static Employee emp3 = new Employee(Department.Accounting,"Counting Savvy", 40);
    static Employee emp4 = new Employee(Department.Accounting,"Counting Novice", 25);
    static Employee emp5 = new Employee(Department.Marketing,"Sales Guru", 50);
    static Employee emp6 = new Employee(Department.Marketing,"Sales Pro", 48);
    static Employee emp7 = new Employee(Department.Marketing,"Sales Savvy", 38);
    static Employee emp8 = new Employee(Department.Human_Resources,"Hiring Guru", 58);
    static Employee emp9 = new Employee(Department.Human_Resources,"Hiring Pro", 47);
    static Employee emp10 = new Employee(Department.Information_Systems,"Hacking Pro", 46);
    static Employee emp11 = new Employee(Department.Information_Systems,"Hacking Guru", 51);
    static Employee emp12 = new Employee(Department.Information_Systems,"Hacking Savvy", 38);
    static Employee emp13 = new Employee(Department.Information_Systems,"Hacking Novice", 23);

    Employee(Department department,String name, int age)
    {
        this.department = department;
        this.name = name;
        this.age = age;
    }


    public int getAge()
    {
        return age;
    }

    public String getName()
    {
        return name;
    }

    public PrimeAgeChecker GetChecker(PrimeAgeChecker checks)
    {
        return checks;

    }

    public void addEmplo(Employee x){
        if (emplo.isEmpty())
        {
            emplo.add(x);
        }
        else
        {
            int i;
            for ( i = 0;i <emplo.size(); ++i){
                if(emplo.get(i).getAge() > x.getAge()){
                    emplo.add(i,x);
                    break;
                }
            }

            if ( i == emplo.size()){
                emplo.add(x);
            }
        }
        }

    public ArrayList<Employee> getEmplo(){
        return emplo;
    }

    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append(getDept(department));
        sb.append("\t");
        sb.append(getName());
        sb.append("\t");
        sb.append(getAge());
        sb.append("\t");
        sb.append(GetChecker(checks));

        return sb.toString();
    }

    private Department getDept(Department department){
        return department;
    }

}




public class PrimeAgeChecker{

    public boolean status = false;

    int ages;

    PrimeAgeChecker(Employee age)
    {
        ages = age.getAge();
    }


    public boolean check(){

            if ((ages % 2 == 0) || (ages == 2))
            {
                status = true;
            }

        return status;

    }
}

Result:

Department      Name    Age Prime
__________________________________________________
Accounting  Counting Guru   55  null
Accounting  Counting Pro    45  null
Accounting  Counting Savvy  40  null
Accounting  Counting Novice 25  null
Marketing   Sales Guru  50  null
Marketing   Sales Pro   48  null
Marketing   Sales Savvy 38  null
Human_Resources Hiring Guru 58  null
Human_Resources Hiring Pro  47  null
Information_Systems Hacking Pro 46  null
Information_Systems Hacking Guru    51  null
Information_Systems Hacking Savvy   38  null
Information_Systems Hacking Novice  23  null
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121

2 Answers2

1

Your method:

public PrimeAgeChecker GetChecker(PrimeAgeChecker checks){
    return checks;
}

Doesn't make sense, because it is merely retuning the very value that it is given. Additionally, you never instantiate the instance variable called checks. Later in your code, when you call GetChecker(checks), since that variable was never instantiated, you're passing null, and subsequently returning null.

If you say it is supposed to be returning a true or false value, then you'll need to:

  1. Set the return type of GetChecker to boolean, and remove the checks parameter from the method signature. Keep in mind, since the GetChecker method is an instance method, and the checks field is also an instance member of the same object, you don't need to pass the parameter in to the method; the method can simply call it directly.
  2. Instantiate the checks member variable (most probably in the constructor)
  3. Given what your PrimeAgeChecker class looks like its doing, I'm guessing you intended to call return checks.check()

Also, some minor suggestions, you've got two typos / naming convention mistakes:

  • Your emplo variable should, I'm guessing, probably be named employees or something along those lines. The getter and setter methods should be similarly renamed.
  • The GetChecker method should have a lower-case first letter, as is the Java standard. This doesn't cause any syntax problems as Java doesn't care, but it is a standard for this language, and it makes your code easier to read by other people (and yourself too) if its consistent.
Paul Richter
  • 10,908
  • 10
  • 52
  • 85
0

You never set the variable checks to anything, therefore when you read it it will have the value null.

I can only guess that you meant to set this variable in your Employee constructor, as so:

Employee(Department department,String name, int age)
{
    this.department = department;
    this.name = name;
    this.age = age;
    this.checks = new PrimeAgeChecker(this);
}

Also, as Christian mentioned, it doesn't make sense to pass a PrimeAgeChecker to GetChecker.

DanielGibbs
  • 9,910
  • 11
  • 76
  • 121