public class Appointment extends DataEntity<Appointment> {
private String orgId; // 所属店ID
private String orgName; // 所属店
private String customerId; // 会员ID
private List<AppointmentItem> appointmentItem;//appointmentItem是从表。
//getset省略不贴在这
}
然后 在方法里拿
List<Appointment> selectAppointment =appointmentService.todayAppointment(appointment);
List<AppointmentItem> selectItem = null;
for(int i = 0;i < selectAppointment.size(); i ++){
if(selectAppointment.get(i).getAppointmentItem()!=null&&!selectAppointment.get(i).getAppointmentItem().isEmpty()){
selectItem.addAll(selectAppointment.get(i).getAppointmentItem());
}
}
问题1:为什么在if判断会报错?
问题2:怎样把selectAppointment 所有 List<AppointmentItem>取出放到selectItem
之后直接selectItem.addAll(selectAppointment.get(i).getAppointmentItem());
你不觉得少了些什么吗,这个null.addAll(selectAppointment.get(i).getAppointmentItem());有什么区别。
应该是List<AppointmentItem> selectItem = new ArrayList<>();
你的第二个问题很简单,你是不是没有弄明白selectItem到底想存的是AppointmentItem还是List<AppointmentItem>???按照你的问题2叙述,存的是List<AppointmentItem>为什么在定义的时候是List<AppointmentItem> selectItem,另外是add而不是addAll。如果确实是存AppointmentItem那你你写的是正确的追问
前台需要List,但是我不想再从数据库取一次从表,所以直接从selectAppointment拿
追答既然是List,那么将List selectItem = null;改成List selectItem = new ArrayList();
再将selectItem.addAll(selectAppointment.get(i).getAppointmentItem());改成selectItem.add(selectAppointment.get(i).getAppointmentItem());
问题2:selectItem 定义的时候要创建对象,不然selectItem.addAll肯定会报空指针