所以我正在尝试做一个测试类,指令是
创建一个名为TestDate的类,其构造函数创建十五个Date对象,并通过调用每个Date的getDayOfTheWeek()方法显示日期的星期几。日期具体是这些日期,按照这个确切的顺序:
但是当我运行它时,我一直都会遇到错误!我认为这是我的构造函数崩溃或沿着那条线的某个地方。
以下是我的TestDate类代码
public class TestDate
{
private Date date;
/*
* Main Constructor
* @param fifteen specific date, test to your heart's content
*/
public TestDate()
{
Date d1 = new Date(1970, 11, 15);
System.out.println(date.getDayOfTheWeek());
Date d2 = new Date(1887, 7, 31);
Date d3 = new Date(1966, 5, 2);
Date d4 = new Date(1980, 8, 19);
Date d5 = new Date(2001, 9, 11);
Date d6 = new Date(1900, 6, 26);
Date d7 = new Date(1940, 2, 28);
Date d8 = new Date(1974, 10, 30);
Date d9 = new Date(1914, 1, 15);
Date d10 = new Date(1840, 10, 1);
Date d11 = new Date(1999, 12, 31);
Date d12 = new Date(1988, 5, 20);
Date d13= new Date(2012, 3, 10);
Date d14 = new Date(2006, 4, 1);
Date d15 = new Date(1992, 2, 29);
}
}
这是我的Date类构造函数和方法getDayofTheWeek,我认为这是与我的问题相关的所有信息。
public class Date
{
public Date(int year, int month, int day)
{
setYear(year);
setMonth(month);
setDay(day);
}
public String getDayOfTheWeek()
{
int step0 = year%100;
int step1 = step0 / 12;
int step2 = step0 % 12;
int step3 = step2 / 4;
int step4 = day;
int step5 = 0;
if(month == JANUARY || month == OCTOBER)
{
step5 = JAN_CODE;
}
else if(month == FEBURARY || month == MARCH || month == NOVEMBER)
{
step5 = FEB_CODE;
}
else if(month == APRIL || month == JULY)
{
step5 = APR_CODE;
}
else if(month == MAY)
{
step5 = MAY_CODE;
}
else if(month == JUNE)
{
step5 = JUN_CODE;
}
else if(month == AUGUST)
{
step5 = AUG_CODE;
}
else if(month == SEPTEMBER || month == DECEMBER)
{
step5 = SEP_CODE;
}
else
{
step5 = 0;
}
if(year/100 ==16 || year/100 == 20)
{
step5 = step5 + SPECIAL_CODE_SIX;
}
else if(year/100 == 17 || year/100 == 21)
{
step5 = step5 + SPECIAL_CODE_FOUR;
}
else if(year/100 == 18)
{
step5 = step5 + SPECIAL_CODE_TWO;
}
int step6 = (step1 + step2 + step3 +step4 + step5)%MOD_CODE;
if(step6 == SAT_CODE)
{
return "Saturday";
}
else if(step6 == SUN_CODE)
{
return "Sunday";
}
else if(step6 == MON_CODE)
{
return "Monday";
}
else if(step6 == TUE_CODE)
{
return "Tuesday";
}
else if(step6 == WED_CODE)
{
return "Wednesday";
}
else if(step6 == THU_CODE)
{
return "Thursday";
}
else if(step6 == FRI_CODE)
{
return "Friday";
}
else
{
return null;
}
}
}
我在mutator方法中传递了参数
public void setYear(int year)
{
if(year <= CURRENT_YEAR && year >= YEAR_ZERO)
{
this.year = year;
}
}
public void setMonth(int month)
{
if(month <= DECEMBER && month >= JANUARY)
{
this.month = month;
}
}
public void setDay(int day)
{
if(month == JANUARY || month == MARCH || month == MAY || month == JULY || month == AUGUST
|| month == OCTOBER || month == DECEMBER)
{
if(day <= DAY_THIRTY_ONE && day >= DAY_ONE)
{
this.day = day;
}
else
{
year = CURRENT_YEAR;
month = JANUARY;
day = DAY_ONE;
}
}
else if(month == APRIL || month == JUNE || month == SEPTEMBER || month == NOVEMBER)
{
if(day <= DAY_THIRTY && day >= DAY_ONE)
{
this.day = day;
}
else
{
year = CURRENT_YEAR;
month = JANUARY;
day = DAY_ONE;
}
}
else if(month == FEBURARY)
{
if((isLeapYear() == true) && day <= DAY_FEB_LEAP && day >= DAY_ONE)
{
this.day = day;
}
else if((isLeapYear() == false) && day <= DAY_FEB_NORMAL && day >= DAY_ONE)
{
this.day = day;
}
else
{
year = CURRENT_YEAR;
month = JANUARY;
day = DAY_ONE;
}
}
}
Sorry for the long list of code, I don't really know which ones are useless.
答案 0 :(得分:1)
在构造函数的第二行,您正在调用
System.out.println(date.getDayOfTheWeek());
在变量date
上,此时为null,因为它尚未初始化。这导致NullPointerException
。请阅读此处以获取更多信息:What is a NullPointerException, and how do I fix it?
我(假设)你想做的是:
System.out.println(d1.getDayOfTheWeek());
除此之外的事情我注意到了:
在没有参数的情况下,您可以在javadoc注释中使用@param
字段。不要这样做。如果你的构造函数没有参数,那么就不应该有@param
。
您正在构造函数中创建15个Date实例,其范围在构造函数完成后立即结束。你可以删除它们,什么都不会改变。我假设你希望这堂课成为一个&#34;考试&#34;您的日期是否已正确初始化。我建议用main
方法(在编码时进行快速测试)和实际的测试框架(如JUnit
)来实现这一目标,以便实现&#39;测试。如果您有兴趣,可以阅读this教程。它还详细说明了您实际想要测试的原因。