-3

So, I can't really get my head around what is going on here. I started by allowing a command line input. I then select the values I want and pass them via instantiation (i think I used that correctly), to be received in the constructor of my second class (MyDate).

They are received and I use: this.day = day; etc.

To set them to my class variables to be used across all methods of the class.

I then have my get methods for day, week, month. They return day, week, month.

So, then I call on my print method, and that was tricky because from what I understand, I had to instantiate the MyCalendarOne class in order to reference a non static method from a static context. So, that done, I referenced the method.

Within that method, I have some print statements and they call upon the methods in the MyDate class via the following code:

System.out.println(myDate.getDay());
System.out.println(myDate.getMonth());
System.out.println(myDate.getYear());

I just don't understand why the values haven't been passed and I am getting the nullpointer error. Which from what I understand is that I'm trying to print 'null'.

Pretty stumped on this one.

I firstly made sure my command line input and parsing was correct. It was.

I actually don't really know what else to try. I had worked through all the other errors but ugh. I'm stumped.

class MyCalendarOne
{
private MyDate myDate;
private enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, 
SATURDAY, SUNDAY};

public static void main(String[] args)
{
    String dateInput = args[0];

    int day = Integer.parseInt(dateInput.substring(0,2));

    int month = Integer.parseInt(dateInput.substring(3,5));

    int year = Integer.parseInt(dateInput.substring(6,10));

    MyDate myDate = new MyDate(day, month, year);

    MyCalendarOne calendar = new MyCalendarOne();

    calendar.printCalendar();


}

/*public MyDate myDate()
{
    return;
}

public Day dayOfWeek()
{

    return;
}

public int weekOfMonth()
{
    return;
} */

public void printCalendar()
{
    System.out.println(myDate.getDay());
    System.out.println(myDate.getMonth());
    System.out.println(myDate.getYear());

}
}

class MyDate
{
private int day;
private int month;
private int year;

public MyDate(int day, int month, int year)
{
    this.day = day;
    this.month = month;
    this.year = year;
}

public int getDay()
{

    return day;
}

public int getMonth()
{
    return month;
}

public int getYear()
{
    return year;
}

public boolean isDateValid()
{
    return true;
}
}

I simply expected it to print out the day, month, and year. However, I got this error back:

java MyCalendarOne 01/01/2011
Exception in thread "main" java.lang.NullPointerException
    at MyCalendarOne.printCalendar(MyCalendarOne.java:45)
    at MyCalendarOne.main(MyCalendarOne.java:22)
  • `MyDate myDate = new MyDate(day, month, year);` declares a *brand new variable* named myDate, which has no relationship to your private field. Your private field never has a value assigned to it, which is why it is null. If you remove the initial `MyDate`, that line of code will assign a value to your private field. – VGR May 16 '19 at 03:53

1 Answers1

1

After you create a new MyDate, you didn't assign that one to your class's myDate. Try adding this.myDate = myDate;

LunaticJape
  • 1,446
  • 4
  • 21
  • 39