无法弄清楚这个java.nullpointerexception

时间:2014-03-11 22:40:13

标签: java nullpointerexception

经过三十分钟的研究并盯着这段代码,我仍然无法弄清楚为什么会出现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;
    }

}
}

仅供参考,日期是我从老师那里获得的.class文件。因此,我不能给出源代码。我知道它包含( - 表示私有,#表示受保护,+表示公共):

-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

1 个答案:

答案 0 :(得分:3)

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

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

此外,您在课堂上也有类似的问题。