Java 即使它没有';不符合条件

Java 即使它没有';不符合条件,java,exception,throw,Java,Exception,Throw,这可能是个很难回答的问题,但我想我会在这里碰碰运气 所以基本上我要做一个作业,它是这样的: 我必须创建一个构造函数,但变量“naam”不能为null或空(“”),变量“Geboortedatam”不能在将来,也不能与今天的日期相同,最后一个变量“boeken”与变量“naam”具有相同的要求(因为它不能为null或“”) 这就是我的构造函数的样子,我只能编辑这一部分,因为另一部分是老师给的,不能编辑 if (this.naam == null || this.naam.equal

这可能是个很难回答的问题,但我想我会在这里碰碰运气

所以基本上我要做一个作业,它是这样的:

我必须创建一个构造函数,但变量“naam”不能为null或空(“”),变量“Geboortedatam”不能在将来,也不能与今天的日期相同,最后一个变量“boeken”与变量“naam”具有相同的要求(因为它不能为null或“”)

这就是我的构造函数的样子,我只能编辑这一部分,因为另一部分是老师给的,不能编辑

        if (this.naam == null || this.naam.equals("")) {
        throw new IllegalArgumentException("Fill in name");
    } else {
        this.naam = naam;
    }
    Date vandaag = new Date();
    if (this.geboorteDatum >= vandaag.getTime()) {
        throw new IllegalArgumentException("Date must be in the past");
    } else {
        this.geboorteDatum = geboortedatum;
    }
    if (this.boeken == null || Arrays.toString(boeken).equals("")) {
        throw new IllegalArgumentException("Can't be empty");
    } else {
        this.boeken = boeken;
    }  
它不断抛出我的第一个异常,我不知道为什么。这可能是一个非常愚蠢的问题,但我似乎不知道为什么


任何帮助都将不胜感激,提前感谢您正在测试的
this.naam
,它是类的实例数据成员。如果这是在一个构造函数中,正如你所说的,那么除非你有一个初始值设定项,否则它可能是
null

您可能打算测试naam,构造函数的参数:

if (naam == null || naam.equals("")) {
//  ^---------------^------------------ no `this.` here
    throw new IllegalArgumentException("Fill in name");
} else {
    this.naam = naam;
}

同样地,对于
Geboortedatam
boeken

@Glenndisimo:我们都做过类似的事情,有时.:-)@Glenndisimo注意到,如果数组为空,
Arrays.toString(boeken).equals(“”)
将永远不会返回
true
。您应该使用
boeken.length==0