我有这个应用程序,我在其中显示事件列表。现在我已经在使用休眠添加、删除和显示它了。我的事件表由 5 个字段组成 - id、name、description、date 和 type。现在我正在尝试添加一个函数,我可以在其中根据类型检索列表。所以我必须为此触发一个正常的查询 -select * from event where event="holiday"
对于类型假期或select * from event where event ="event"
类型事件。现在,当我尝试这样做时,它会引发错误。我在这里附上我的代码。
package net.admin.module.view;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.classic.Session;
import javax.servlet.http.HttpServletRequest;
import net.admin.module.dao.EventDAO;
import net.admin.module.dao.HolidaysDAO;
import net.admin.module.model.Event;
import net.admin.module.model.Holidays;
import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONObject;
import com.opensymphony.xwork2.ActionSupport;
public class EventAction extends ActionSupport {
private static final long serialVersionUID = 9149826260758390091L;
private Event event;
private List<Event> eventList;
private Holidays holidays;
private List<Holidays> holidaysList;
private HolidaysDAO holidaysDao;
private EventDAO eventDao;
private org.hibernate.Session session;
private List<?> list;
public String execute() {
HttpServletRequest request = ServletActionContext.getRequest();
String type = request.getParameter("type");
if(type!=null && (type.equalsIgnoreCase("holidays") || type.equalsIgnoreCase("event"))){
session = null;
//
Query query = session.createQuery("from s360_event where type = 'event' ");
list = query.list();
}else{
this.eventList = eventDao.list();
}
System.out.println("execute called");
return SUCCESS;
}
public String cal(){
return "calendar";
}
public String eventAjax() {
this.eventList = eventDao.list();
HttpServletRequest request = ServletActionContext.getRequest();
JSONArray jArr = new JSONArray();
try{
JSONObject jObject = null;
Event event = null;
for(int i = 0; i < eventList.size(); i++) {
event = eventList.get(i);
jObject=new JSONObject();
jObject.put("title", event.getName());
jObject.put("start", event.getDate());
jObject.put("allDay", true);
jArr.put(jObject);
}
request.setAttribute("json", jArr);
}
catch(Exception e){
e.printStackTrace();
}
return "json";
}
// to add an event
public String add() {
System.out.println(getEvent());
try {
Event eve = getEvent();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
eve.setDate(df.parse(eve.getDateStr()));
} catch (ParseException e) {
e.printStackTrace();
}
eventDao.add(eve);
} catch (Exception e) {
e.printStackTrace();
}
return "redirect";
}
// to delete an event
public String delete() {
HttpServletRequest request = ServletActionContext.getRequest();
String eventId = request.getParameter("id");
eventDao.delete(Integer.parseInt(eventId));
return "redirect";
}
public Event getEvent() {
return event;
}
public List<Event> getEventList() {
return eventList;
}
public void setEvent(Event event) {
this.event = event;
}
public void setEventList(List<Event> eventList) {
this.eventList = eventList;
}
public Holidays getHolidays() {
return holidays;
}
public List<Holidays> getHolidaysList() {
return holidaysList;
}
public void setHolidays(Holidays holidays) {
this.holidays = holidays;
}
public void setHolidaysList(List<Holidays> holidaysList) {
this.holidaysList = holidaysList;
}
public HolidaysDAO getHolidaysDao() {
return holidaysDao;
}
public void setHolidaysDao(HolidaysDAO holidaysDao) {
this.holidaysDao = holidaysDao;
}
public EventDAO getEventDao() {
return eventDao;
}
public void setEventDao(EventDAO eventDao) {
this.eventDao = eventDao;
}
}
这是我的行动课。我在我的jsp中这样称呼它:
<li><a href="<s:url value='/Event?type=event'/>">Events</a></li>
<li><a href="<s:url value='/Event?type=holidays'/>">Holidays</a></li>
</li>
关于我哪里出错的任何线索?
错误发生 -
java.lang.NullPointerException
net.admin.module.view.EventAction.execute(EventAction.java:38)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)