sM is an instance of the class, When running the method staffCanWork it is supposed to return a List of staff objects that can work on the given date and time, however when running the method it null pointer errors.
Testing the method within static void main;
Date date = Date.valueOf(LocalDate.now());
List<Staff> staff = sM.staffCanWork(date, Time.valueOf(LocalTime.now()));
for(int i = 0; i < staff.size(); i++){
System.out.println(staff.get(i).getId());
}
Method getStaff() using hibernate populates the List with staff objects using hibernate.
public List<Staff> getStaff(){
List staff =null;
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
TypedQuery query = session.getNamedQuery("getStaff");;
staff = query.getResultList();
}catch (HibernateException e){
e.printStackTrace();
}return staff;
}
staffCanWork method, Checks through the list to see if staff can work the given date and time.
public List<Staff> staffCanWork(Date date, Time time){
List<Staff> staff = getStaff();
List<Staff> staff1 = null;
for(int i = 0; i < staff.size(); i++){
if(staff.get(i).getDoctor() == null){
if(staff.get(i).getNurse().canWork(date, time)) staff1.add(staff.get(i));
}
else {
if(staff.get(i).getDoctor().canWork(date, time)) staff1.add(staff.get(i));
}
}
System.out.println(staff1.get(0).getId() + "test");
return staff1;
}
Method CanWork
public boolean canWork(Date date, Time time){
Boolean canWork =false;
Boolean aM = false;
if(time.getTime() < 12){ aM = true;}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E");
simpleDateFormat.format(date);
if(aM = true) {
switch (simpleDateFormat.format(date)) {
case "Mon": if (getMondayAM() == true) canWork = true;
break;
case "Tue" :if (getTuesdayAM() == true) canWork = true;
break;
case "Wed" :if (getWednesdayAM() == true) canWork = true;
break;
case "Thu" :if (getThursdayAM() == true) canWork = true;
break;
case "Fri" :if (getFridayAM() == true) canWork = true;
break;
case "Sat" :if (getSaturdayAM() == true) canWork = true;
break;
case "Sun" :if (getSundayAM() == true) canWork = true;
default: return false;
}
}
switch (simpleDateFormat.format(date)) {
case "Mon": if (getMondayPM() == true) canWork = true;
break;
case "Tue" :if (getTuesdayPM() == true) canWork = true;
break;
case "Wed" :if (getWednesdayPM() == true) canWork = true;
break;
case "Thu" :if (getThursdayPM() == true) canWork = true;
break;
case "Fri" :if (getFridayPM() == true) canWork = true;
break;
case "Sat" :if (getSaturdayPM() == true) canWork = true;
break;
case "Sun" :if (getSundayPM() == true) canWork = true;
default: return false;
}
return canWork;
}