事件发生时空指针异常

时间:2015-12-05 11:34:16

标签: java timer hibernate-session

计划的计时器事件发生时的异常..... 当我使用SetEvent se = new SetEvent();         se.startArchiveEvent(); 然后我得到了例外......

Exception in thread "Timer-0" java.lang.NullPointerException
    at com.metacube.metice.dao.AbstractDao.getSession(AbstractDao.java:47)
    at com.metacube.metice.dao.AbstractDao.createEntityCriteria(AbstractDao.java:120)
    at com.metacube.metice.dao.NoticeDaoImpl.archiveExpiredNotices(NoticeDaoImpl.java:162)
    at com.metacube.metice.service.ArchiveEvent.run(ArchiveEvent.java:15)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)

以下是相关的课程

ArchiveEvent.java

 package com.metacube.metice.service;

    import java.util.TimerTask;

    import org.springframework.beans.factory.annotation.Autowired;

    import com.metacube.metice.dao.NoticeDao;
    import com.metacube.metice.dao.NoticeDaoImpl;

    public class ArchiveEvent extends TimerTask{
      // @Autowired
       NoticeDao noticeDao = new NoticeDaoImpl();
        @Override
        public void run() {
            noticeDao.archiveExpiredNotices();
        }

    }

SetEvent.java

package com.metacube.metice.service;


import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContextEvent;

public class SetEvent{
    public void startArchiveEvent(){
        TimerTask archiveNotices = new ArchiveEvent();
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(archiveNotices, nextMidNight(), 1000*60*60*24);
        System.out.println("Automated archive Notice event started[period 24 Hours]...");
    }


    private static Date nextMidNight(){
        Calendar cal = Calendar.getInstance();
        Calendar midNight = new GregorianCalendar(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DATE),16,43,00);
        System.out.println(midNight.getTime());
        return midNight.getTime();

      }


}

AbstractDao.java

package com.metacube.metice.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @name AbstractDao
 * @description : This is abstract generic class which performs all hibernate
 *              CRUD operation of persistant class into database
 * @author Banwari
 *
 * @param <PK>
 *            : primary key field of persistent class
 * @param <T>
 *            : name of pesrsitent class type
 * @date 03-Dec-2015
 */
@Transactional
public abstract class AbstractDao<PK extends Serializable, T> {
    /* Generic class type varible */
    private final Class<T> persistentClass;

    /* default constructor which makes generic class to specific persitent class */
    @SuppressWarnings("unchecked")
    public AbstractDao() {
        this.persistentClass = (Class<T>) ((ParameterizedType) this.getClass()
                .getGenericSuperclass()).getActualTypeArguments()[1];
    }

    /* SessionFactory class object */
    @Autowired
    private SessionFactory sessionFactory;

    /**
     * This method for getting hibernate Session object reference
     * 
     * @return current session
     */
    protected Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    /**
     * This method for getting persistent object from database for primary key
     * primary key
     * 
     * @param key
     *            : Primary Key type of persistent class
     * @return persistent object
     */
    @SuppressWarnings("unchecked")
    public T getByKey(PK key) {
        return (T) getSession().get(persistentClass, key);
    }

    /**
     * This method save object into database
     * 
     * @param entity
     *            : is object which have to be save into the database
     * @throws HibernateException
     */
    public void save(T entity) throws HibernateException {
        getSession().save(entity);
    }

    /**
     * This method persist object into database
     * 
     * @param entity
     *            : is object which have to be persist into the database
     * @throws HibernateException
     */
    public void persist(T entity) throws HibernateException {
        getSession().persist(entity);
    }

    /**
     * This method saveOrUpdate object into database
     * 
     * @param entity
     *            : is object which have to be save or update into the database
     * @throws HibernateException
     */
    public void saveOrUpdate(T entity) throws HibernateException {
        getSession().saveOrUpdate(entity);
    }
    /**
     * This method delete object from database
     * 
     * @param entity
     *            : is object which have to be delete from the database
     * @throws HibernateException
     */
    public void delete(T entity) {
        getSession().delete(entity);
    }
    /**
     * This method update object into database
     * 
     * @param entity
     *            : is object which have to be update into the database
     * @throws HibernateException
     */
    public void update(T entity) throws HibernateException{
        getSession().update(entity);
    }
    /**
     * This method for getting Criteria object refernce
     * @return Criteria for the persistentClass
     */
    protected Criteria createEntityCriteria() {
        return getSession().createCriteria(persistentClass);
    }

}

0 个答案:

没有答案