簡體   English   中英

即使不符合條件,也要保持拋出異常

[英]Keeps throwing Exception even if it doesn't meet the condition

這可能是一個非常棒的問題,但我想我會抓住機會。

所以基本上我必須做一個任務,它如下:

我必須創建一個構造函數,但變量“naam”不能為null或為空(“”),變量“geboortedatum”不能在將來也不能與今天和最后一個變量相同“ 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;
}

同樣對於geboortedatumboeken

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM