gpt4 book ai didi

java - 无法弄清楚这个 java.nullpointerexception

转载 作者:行者123 更新时间:2023-11-29 05:27:02 27 4
gpt4 key购买 nike

经过三十分钟的研究和盯着这段代码,我仍然无法弄清楚为什么会出现 java.nullpointerexception 错误。这是创建 LongDate(我制作的一个类)对象数组的主程序。如果错误可能在其他类中,请索取代码,我可以给你。谢谢。

public class Assignment1 {

public static void main(String[] args) {

//creates an array of type LongDate filled with two LongDate objects

LongDate [] collectionOfDates = { new LongDate("February",2,1996), new LongDate("November",13,1999) };

// loops through the array and displays output of getDate() for each object

for( int i = 0; i < collectionOfDates.length; i++ ) {

System.out.println( collectionOfDates[i].getDate() );

}

}

}

此外,这是 LongDate 的代码。

public class LongDate extends Date {

private String monthName;
private int month;

public LongDate() {

}

public LongDate(String m, int d, int y) {

if (monthName.equals("January")) {
month = 1;
} else if (monthName.equals("February")) {
month = 2;
} else if (monthName.equals("March")) {
month = 3;
} else if (monthName.equals("April")) {
month = 4;
} else if (monthName.equals("May")) {
month = 5;
} else if (monthName.equals("June")) {
month = 6;
} else if (monthName.equals("July")) {
month = 7;
} else if (monthName.equals("August")) {
month = 8;
} else if (monthName.equals("September")) {
month = 9;
} else if (monthName.equals("October")) {
month = 10;
} else if (monthName.equals("November")) {
month = 11;
} else if (monthName.equals("December")) {
month = 12;
} else
month = 0;

super.setDate(month,d,y);

monthName = editMonth(monthName);
super.editDay(d);
super.editYear(y);

}

public void setDate(String m, int d, int y) {

if (monthName.equals("January")) {
month = 1;
} else if (monthName.equals("February")) {
month = 2;
} else if (monthName.equals("March")) {
month = 3;
} else if (monthName.equals("April")) {
month = 4;
} else if (monthName.equals("May")) {
month = 5;
} else if (monthName.equals("June")) {
month = 6;
} else if (monthName.equals("July")) {
month = 7;
} else if (monthName.equals("August")) {
month = 8;
} else if (monthName.equals("September")) {
month = 9;
} else if (monthName.equals("October")) {
month = 10;
} else if (monthName.equals("November")) {
month = 11;
} else if (monthName.equals("December")) {
month = 12;
} else
month = 0;

super.setDate(month,d,y);

monthName = editMonth(monthName);
super.editDay(d);
super.editYear(y);

}


public String getDate() {

StringBuilder fullDate = new StringBuilder();
fullDate.append(monthName);
fullDate.append(" ");
fullDate.append(getDay());
fullDate.append(", ");
fullDate.append(getYear());

return fullDate.toString();
}

public String getShortDate() {

StringBuilder shortDate = new StringBuilder();
shortDate.append(month);
shortDate.append("/");
shortDate.append(getDay());
shortDate.append("/");
shortDate.append(getYear());

return shortDate.toString();
}

protected String editMonth(String m) {

if (month == 0) {
m = Input.getString( "Invalid month. Please type the month again." );
return m;
} else {
return m;
}

}
}

仅供引用,Date 是我的老师给我的一个 .class 文件。因此,我无法提供源代码。我知道它包含(- 表示私有(private),# 表示 protected ,+ 表示公开):

-month: int
-day: int
-year: int

#editMonth(int m): int
#editDay (int d): int
#editYear (int y) : int
+Date()
+setDate(int m, int d, int y): void
+Date( int m, int d, int y)
+getDate(): String
+getMonth(): int
+getDay(): int
+getYear(): int

最佳答案

正如之前的发帖人所说,错误出在您的 LongDate 构造函数中。问题出在 if 表达式中。变量 monthName 尚未初始化。相反,它应该是 m,因为这是参数。

if (monthName.equals("January")) {
month = 1;
} else if (monthName.equals("February")) {

另外,你在类里面也遇到了类似的问题。

关于java - 无法弄清楚这个 java.nullpointerexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22338203/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com