0

I have two classes (Date and Employee).

Class Date doesn't have a constructor, but it has 3 variables with their setters.

Class Employee has a constructor where I initialized its variables. But I have to attach the 3 variables in Class Date to it.

I tried using Setter method, but when I run the code, it says that there's an error with:

hire_date.setDay(1);
hire_date.setMonth(1); 
hire_date.setYear(2018);  

//In class Empolyee

 private String name;
 private Date hire_date;
 private double monthly_salary; 

    public Employee() {
        name = "Jody";
        hire_date.setDay(1);
        hire_date.setMonth(1); 
        hire_date.setYear(2018);         
        monthly_salary = 2000.0;
    }

//In class Date

 private int day;   
 private int month;
 private int year;

    public void setDay(int day) {
        this.day = day;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public void setYear(int year) {
        this.year = year;
    }

3 Answers3

1

There's a new Date() missing. Your Date object does not exist when you invoke the setters

Paul Janssens
  • 622
  • 3
  • 9
1

You have not created a Date object. Therefore, how you should be able to set its properties?

Date hire_date = new Date();
arxakoulini
  • 723
  • 7
  • 22
1

I run this program. Error in here is a Null Pointer exception.

Reason for it is that hire_date is not initialized. You need to add: hire_date = new Date();

before first usage of hire_date

Przemek
  • 126
  • 1
  • 6