0

I have class date which just simply creates an arrayList of days of the week.

import java.util.*;

public class Date {

    public static final String[] daysofTheWeek = {"Mon" , "Tue" , "Wed" , "Thurs" , "Fri" , "Sat" , "Sun"};
    private String dayofweek;
    private int day;
    private int month;

    public int getDate() {
        return this.day;
    }

    public int getMonth() {
        return this.month;
    }

    public Date(String dayofweek, int day, int month) {
        this.day = 1;
        if (day >= 1 && day <= 31) {
            this.day = day;
        }

        this.month = 12;
        if (month >= 1 && month <= 12) {
            this.month = month;
        }

        this.dayofweek = "Mon";
        if (isValidDOW(dayofweek)) { // why is this in the parameters?
            this.dayofweek = dayofweek;
        }
    }

    // must validate is DOW 
    //  TRUE/FALSE is the day of the week matching?
    public boolean isValidDOW(String d) {
    for (int i = 0; i < dayofweek.length(); i++)
        if (d.equals(daysofTheWeek[i])) { // why i?
            return true;
        }
        return false;
    }

    // to string
    public static String monthToString(int i) {
        switch(i) {
            case 1: return "January";
            case 2: return "Feb";
            case 3: return "March";
            case 4: return "April";
            case 5: return "May";
            case 6: return "June";
            case 7: return "July";
            case 8: return "August";
            case 9: return "September";
            case 10: return "October";
            case 11: return "November";
            case 12: return "December";
            default: return "Invalid Month!!";
        }
    }

    public String toString() {
        return this.dayofweek + "," + Date.monthToString(this.month) + " " + this.day;
    }

    public String getDayOfWeek() {
        return this.dayofweek;
    }
}

Now I am trying to get all days ( Say, return number of times sundays pop up). I tried using a loop to go through and count everything, but I am getting a null pointer exception. I've been looking at this for a while, and can't figure out whats wrong with it. Can anyone guide me in the right direction?

import java.util.*;
/**
 * ArrayList example
 */
public class Calendar {

    private ArrayList<Date> Schedule;

    public ArrayList<Date> sundays(String dayofweek) {
        ArrayList<Date> app = new ArrayList<Date>();
        for (Date entry : Schedule) {
            String d = entry.getDayOfWeek();
            if (entry.getDayOfWeek() == dayofweek) {
                app.add(entry);
            }
            else if (app.size() == 0) {
                return null;
            }
        }
        return app;
    }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
Susan
  • 1
  • 2

2 Answers2

0

You never initialize the field Schedule, so it is default-initialized to null. Therefore, the line for (Date entry:Schedule) (which is syntactic sugar for calling Schedule.iterator() and iterating over the resulting iterator) throws a NullPointerException.

Also — non-static field names in Java are supposed to start with lowercase letters. You should change Schedule to schedule. (Or you can follow one of the more-specific naming conventions that exist out there. Some projects start all field-names with f — in your case fSchedule; others start all field-names with m for "member" — in your case, mSchedule. If you adopt one of those conventions, just be sure to be consistent about it.)

ruakh
  • 175,680
  • 26
  • 273
  • 307
0

I think the method isValidDOW should be:

public boolean isValidDOW(String d){
    for  (int i=0; i < daysofTheWeek.length; i++)
        if ( d.equals(daysofTheWeek[i])) // why i?
        {
            return true;
    }
    return false;
}

Using daysofTheWeek.length instead of dayofweek.length(), for example if dayofweek = 'Sat', the for loop will only execute 3 times, but 'Sat' is in the 6th place and the value is 5 in ArrayList daysofTheWeek.

zhouji
  • 5,056
  • 1
  • 26
  • 26