简体   繁体   English

无法弄清楚这个java.nullpointerexception

[英]Can't figure out this java.nullpointerexception

after thirty minutes of research and staring at this code, I still cannot figure out why a java.nullpointerexception error occurs. 经过三十分钟的研究并盯着这段代码,我仍然无法弄清楚为什么会出现java.nullpointerexception错误。 This is the main program that creates an array of LongDate (A class that I made) objects. 这是创建LongDate(我制作的类)对象数组的主程序。 If the error might be in the other classes, ask for the code and I can give it to you. 如果错误可能在其他类中,请询问代码,我可以将其提供给您。 Thanks. 谢谢。

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() );

    }

}

}

Also, here is the code for LongDate. 此外,这是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;
    }

}
}

FYI, Date is a class that I was given as a .class file from my teacher. 仅供参考,日期是我从老师那里获得的.class文件。 Thus, I cannot give the source code. 因此,我不能给出源代码。 I do know that it contains (- means private, # means protected, and + means public): 我知道它包含( - 表示私有,#表示受保护,+表示公共):

-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

As previous posters said, the error is in your LongDate constructor. 正如之前的海报所说,错误发生在你的LongDate构造函数中。 The problem is in the if expression. 问题出在if表达式中。 The variable monthName has not be initialized. 变量monthName尚未初始化。 Instead, it should be m as that is the parameter. 相反,它应该是m因为那是参数。

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

Also, you have similar problems through the class. 此外,你在课堂上也有类似的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM