在struts 2应用程序中使用hibernate查询列表的问题

时间:2013-11-11 06:22:30

标签: hibernate struts2

我有这个应用程序,我正在显示事件列表。现在我已经使用hibernate添加,删除和显示它了。我的事件表由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)

1 个答案:

答案 0 :(得分:1)

在执行时,“会话”在行号为38时为空

Query query = session.createQuery("from s360_event where type = 'event' ");

因为你是初学者。它在第36行为空

session = null;

这就是Exception "java.lang.NullPointerException"

的原因