我不知道为什么我的代码给我java.util.date一个空值。

问题:编写一个测试程序,创建一个Account对象,该对象的帐户ID为1122,余额为20000,年利率为4.5%。使用提现方法提取$ 2500,使用存款方法存入$ 3000,并打印余额,每月利息和创建此帐户的日期
这是我的代码:

import java.util.*;
public class Account {
private int ID;
private double Balance;
private double annualInterestRate;
private java.util.Date dateCreated;
public Account(){}
public Account(int ID, double Balance, double annualInterestRate){
    this.ID=ID;
    this.Balance=Balance;
    this.annualInterestRate= annualInterestRate;
}
public void setID(int ID){
    this.ID=ID;
}
public void setBalance(double Balance){
    this.Balance=Balance;
}
public void setAnnualInterestRate(double annualInterestRate){
    this.annualInterestRate= annualInterestRate;
}
public int getID(){
    return ID;
}
public double getBalance(){
    return Balance;
}
public double getInterestRate(){
    return annualInterestRate;
}
public java.util.Date getDateCreated(){
    return dateCreated;
}
public double getMonthlyInterestRate(){
    return annualInterestRate/12;
}
public void withDraw(double val){
    if ((Balance - val) <0)
    {
        System.out.println("Offensive content removed from this line");
    }
    else
    {
        Balance -= val;
    }
}
public void dePosits(double value){
    Balance += value;
}

public static void main(String [] arges){
Account account = new Account(1122, 20000,.045);
account.withDraw(2500);
account.dePosits(3000);
System.out.println(account.getBalance());
System.out.println(account.getDateCreated());
}


}

最佳答案

您尚未初始化dateCreated。例如,在构造函数中(或其他位置,具体取决于您的用例)

dateCreated = new java.util.Date();


或任何其他初始化日期的方式。

09-29 22:20