after thirty minutes of research and staring at this code, I still cannot figure out why a java.nullpointerexception error occurs. This is the main program that creates an array of LongDate (A class that I made) objects. 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.
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. 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. The problem is in the if
expression. The variable monthName
has not be initialized. Instead, it should be m
as that is the parameter.
if (monthName.equals("January")) {
month = 1;
} else if (monthName.equals("February")) {
Also, you have similar problems through the class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.