0

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
Plaidypus
  • 81
  • 9
  • `startTime` is probably null. Post the code where you assign/initialize that variable. – ApproachingDarknessFish Nov 07 '14 at 23:08
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – khelwood Nov 07 '14 at 23:13
  • @khelwood: Funny, and maybe true, but maybe a little to generic to keep "possible duplicate" at that level? – mattias Nov 07 '14 at 23:19
  • I've exhausted many options so I apologize if this is on a beginner level (because that's what I am). I added "this." to the beginning of my startTime, but that doesn't fix anything. **Edit**: Initializing the startTime and endTime fixed me issue. Thank you guys/girls! – Plaidypus Nov 07 '14 at 23:21
  • @Plaidypus: If you use Eclipse or something similar, this can be a good practise for some debugging. Put a breakpoint somewhere in your program and step through the code and try to read your variables' values. – mattias Nov 07 '14 at 23:22
  • @mattias Not really. The entire contents of the post were irrelevant except that he/she was getting an NPE and didn't know what to do about it. – khelwood Nov 07 '14 at 23:29

2 Answers2

0

I believe your problem is with startTime.setDate(month, day, year, hour, minute);

Are you sure startTime is ever initialized?

austin wernli
  • 1,801
  • 12
  • 15
0
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);
}

What you are outlining as Default Time is not working like this. EVERY Object that is just declared is null. You need to initialize it, before acecssing its properties (or calling methods ON the object - you still can pass it arround as a parameter ofc)

protected Time startTime

is a declaration

protected Time startTime = new Time();

is a declaration followed by initialization. Until you initialize an object it is null and cannot be used for access unless a value has been assigned to that declaration.

if you don't want to initialize an object unless you really need it, you should consider using:

public void setStart(String month, int day, int year, int hour, int minute)
{
    if (this.startTime == null){
       this.startTime = new Time();
    }
    this.startTime.setDate(month, day, year, hour, minute);
}

ps.: Depending on what kind of Time Class you are using, there might be no Constructor without arguments.

dognose
  • 20,360
  • 9
  • 61
  • 107
  • Dammit! I must have deleted that: 'new Time();' when I was getting rid of excess code. Thank you so much! – Plaidypus Nov 07 '14 at 23:23