Question

this might be a really noob question but I thought I'd take my chances here.

So basically I have to make an assignment and it goes as following:

I have to create a constructor but the variable "naam" can't be null or empty (""), the variable "geboortedatum" can't be in the future nor can it be the same date as today and the last variable "boeken" has the same requirements as the variable "naam" (being it can't be null nor "").

So this is what my constructor looks like, I can only edit this part because the other part is given by our teacher and can't be edited.

        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;
    }  

It keeps throwing my first exception, and I can't figure out why. This is probably a really stupid question but I can't seem to find out why.

Any help would be much appreciated, thanks in advance

Was it helpful?

Solution

You're testing this.naam, which is the instance data member of the class. If this is in a constructor as you've said, then unless you have an initializer on it, it's probably null.

You probably meant to be testing naam, the argument to the constructor:

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

And similarly for geboortedatum and boeken.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top