Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java primeagechecker的空值_Java - Fatal编程技术网

Java primeagechecker的空值

Java primeagechecker的空值,java,Java,很抱歉发了这么多次。我陷入了为什么结果为空的困境。这应该是真的还是假的。我认为检查值在Employee类中没有链接。 请告诉我问题出在哪里 public class Employee { String name; PrimeAgeChecker checks; int age; Department department; public ArrayList<Employee> emplo; static Employee emp1 =

很抱歉发了这么多次。我陷入了为什么结果为空的困境。这应该是真的还是假的。我认为检查值在Employee类中没有链接。 请告诉我问题出在哪里

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;

    }
}

您从未将变量
checks
设置为任何值,因此当您读取它时,它的值将为
null

我只能猜测您打算在
Employee
构造函数中设置此变量,如下所示:

Employee(Department department,String name, int age)
{
    this.department = department;
    this.name = name;
    this.age = age;
    this.checks = new PrimeAgeChecker(this);
}
此外,正如Christian所提到的,将
PrimeAgeChecker
传递给
GetChecker

您的方法是没有意义的:

public PrimeAgeChecker GetChecker(PrimeAgeChecker checks){
    return checks;
}
没有意义,因为它只是重新调整给定的值。此外,您从未实例化名为
checks
的实例变量。在代码的后面,当您调用
GetChecker(checks)
时,由于该变量从未被实例化,所以您将传递null,然后返回null

如果您说它应该返回
true
false
值,那么您需要:

  • GetChecker
    的返回类型设置为
    boolean
    ,并从方法签名中删除
    checks
    参数。请记住,由于
    GetChecker
    方法是一个实例方法,并且
    checks
    字段也是同一对象的实例成员,因此不需要将参数传递给该方法;该方法可以直接调用它
  • 实例化
    检查
    成员变量(最可能在构造函数中)
  • 考虑到PrimeAgeChecker类的功能,我猜您打算调用
    返回checks.check()
  • 还有一些小建议,您有两个拼写错误/命名约定错误:

    • 我猜,您的
      emplo
      变量应该被命名为
      employees
      或类似的名称。getter和setter方法应该类似地重命名
    • 与Java标准一样,
      GetChecker
      方法的第一个字母应该小写。这不会导致任何语法问题,因为Java并不关心,但它是这种语言的标准,如果代码一致,它会使您的代码更容易被其他人(以及您自己)阅读

    现在,我如何对部门和员工姓名进行排序?你有什么想法让我从开始吗?@fransraharja
    public PrimeAgeChecker GetChecker(PrimeAgeChecker checks){
        return checks;
    }