I've been trying to implement a super class to reduce the length of code in my program. The program is an activity planner, where the user will decide whether they want to input a Home, School or Other activity. Right now, I am trying to add a partially completed instance to an ArrayList, and then I am trying to access that partially completed instance and give it a Start Time and End Time. Here is some of the code:
ArrayList is created by: ArrayList<Activity> aList = new ArrayList();
Main method:
System.out.println("*****************");
System.out.println("**Home Activity**");
System.out.println("*****************");
System.out.println("\n** Name **");
String tempName = keyboard.nextLine();
System.out.println("**Description **");
String tempDescr = keyboard.nextLine();
aList.add(new Activity(tempName, tempDescr, "Home"));
main.startTime(aList, listCount); // This line is where I get my error
main.endTime(aList, listCount);
// Methods aren't static so I need instance of class (main)
Further down is my startTime method:
// Creates a startTime for the instance
public void startTime(ArrayList<Activity> list, int listCount)
{
DayPlanner main = new DayPlanner();
System.out.println("\n******Start Time*******");
int intYear = main.yearInput();
String tempMonth = main.monthInput();
int intDay = dayInput();
int intHour = hourInput();
int intMinute = minuteInput();
list.get(listCount).setStart(tempMonth, intDay, intYear, intHour, intMinute);
// Line above is also where I get my error
}
setStart is in my Activity Class:
public class Activity {
protected String type;
protected String activityName;
protected Time startTime; // Default Time
protected Time endTime; // Default Time
protected String activityDescription;
public void setStart(String month, int day, int year, int hour, int minute)
{
this.startTime.setDate(month, day, year, hour, minute);
}
And setDate is in my Time Class:
public void setDate(String month, int day, int year, int hour, int minute)
{
if (dateOK(month, day, year, hour, minute))
{
this.month = month;
this.day = day;
this.year = year;
this.hour = hour;
this.minute = minute;
}
else
{
System.out.println("Improper date set. Please try again.");
System.exit(0);
}
}
I apologize for hopping around but I can't seem to figure out what I'm doing wrong! The problem I'm having is a NullPointerException.
Here is the output of my error. As you can see, I am only able to input up to the end of my Start Time, so when I try to assign the Start Time to the element in the ArrayList, something happens.
******Start Time*******
** Year **
2000
** Month **
august
** Day **
09
** Hour **
5
** Minute **
61
Not valid minute. Range is [0-59].
** Minute **
59
Exception in thread "main" java.lang.NullPointerException
at pkg2430ass1.Activity.setStart(Activity.java:50)
at pkg2430ass1.DayPlanner.startTime(DayPlanner.java:304)
at pkg2430ass1.DayPlanner.main(DayPlanner.java:50)
Java Result: 1