0

i have this app where i am displaying a list of events. now i am already adding , deleting and displaying it using hibernate. my event table consists of 5 fields - id , name , description , date and type . now i am trying to add a function where i can retrieve a list based on the type. So i have to fire a normal query for that - select * from event where event="holiday" for the type holidays or select * from event where event ="event" for the type event. Now when I'm trying to do it it throws an error. I'm attaching my code here.

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;
    }
    }

this is my action class. and I'm calling it this way in my 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>

Any clues on where I'm going wrong ?

The error goes -

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)
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
struts2
  • 54
  • 1
  • 7

1 Answers1

1

At the time of execution "session" is null at line number : 38

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

Because you are init. it null at line number 36

session = null;

and that's why there is Exception "java.lang.NullPointerException"

Pratik Shah
  • 1,782
  • 1
  • 15
  • 33