0

When I use @Autowired annotation for SessionFactory to retrive data from the DB when using DHXEventsManager,java.lang.NullPointerException occurred.I can't fix it.Please anyone help!

package com.dhtmlx.booking.controller;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.dhtmlx.planner.DHXEv;
import com.dhtmlx.planner.DHXEventsManager;
import com.dhtmlx.planner.DHXStatus;
import com.dhtmlx.planner.data.DHXCollection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CustomEventsManager extends DHXEventsManager {
 @Autowired
 SessionFactory sessionfactory;
        public CustomEventsManager(HttpServletRequest request) {
                        super(request);
        }

        public Iterable<DHXEv> getEvents() {

                        List<DHXEv> evs = new ArrayList<DHXEv>();
                        try {

 evs = sessionfactory.getCurrentSession().createCriteria(Event.class).list();
                        } catch (RuntimeException e) {
                                        e.printStackTrace();
                        } 

        return evs;
        }
      }
Horizon_Net
  • 5,959
  • 4
  • 31
  • 34
  • You haven't instantiated a `sessionfactory` - it is null by default, hence the null pointer exception. – Aleksander Panayotov Jun 22 '15 at 19:01
  • do you have a `sessionFactory` bean? – Jos Jun 22 '15 at 19:12
  • Show the class where you call `getEvents()`. – chrylis -cautiouslyoptimistic- Jun 22 '15 at 19:16
  • I note that you don't have any Spring annotations on the class at all, which makes me strongly suspect that you're just calling `new CustomEventsManager()` at the point of service. Instantiating a new copy of the manager object for every request looks like a bad design, but if it's unavoidable, you should pass the `SessionFactory` as a constructor argument. – chrylis -cautiouslyoptimistic- Jun 22 '15 at 19:22
  • [link]This is (http://javaplanner.com/docs/using_javaplanner_with_the_spring_framework.html) the correct code. i had changed the Dispatcher-servlet since i wanted to configure hibernate in my Dispatcher-servlet.xml file. While trying to accessing session from sessionFactory I'm getting a null pointer Exception – sharath valiyapurayil Jun 23 '15 at 07:51

0 Answers0