查询数据库时出现nullpointer异常

时间:2013-04-20 09:06:20

标签: java database hibernate session

我试图从数据库中查询一个hibernate项目

public class FeedInputParamsDaoImpl implements IFeedInputParamsDao {
 @Autowired
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

private int BATCH_SIZE = 50;

public Session getSession() {
/*null pointer exception*/Session session = sessionFactory.openSession();
    return session;
}
      @Override
public List<FeedInputParams> getAllFeedInputParamslist (int feedKey) {
    String queryString = "from FeedInputParams as feedInputParams where feedInputParams.feedKey > ?";
    Session session =  getSession();
    Query query = session.createQuery(queryString);
    query.setParameter(0, feedKey);
    List<FeedInputParams> feedInputParamsList = query.list();
    return feedInputParamsList;
}

}

从方法中调用 getAllFeedInputParamslist(int feedKey)

  public void readFile()
        throws Exception {  
    FeedInputParamsDaoImpl feedInputParamsDaoImpl=new FeedInputParamsDaoImpl();
    List<FeedInputParams> feedInputParamsList=feedInputParamsDaoImpl.getAllFeedInputParamslist(0);
    System.out.println("feedInputParamsList...."+feedInputParamsList);

}

我收到空指针异常

java.lang.NullPointerException at com.vxl.appanalytix.dao.impl.FeedInputParamsDaoImpl.getSession(FeedInputParamsDaoImpl.java:28) 
 at com.vxl.appanalytix.dao.impl.FeedInputParamsDaoImpl.getAllFeedInputParamslist(FeedInputParamsD0Impl.java:44) 
at

1 个答案:

答案 0 :(得分:0)

SessionFactory为null - 由于某种原因,它不是由spring自动装配的。你有没有关于FeedInputParamsDaoImpl类的注释?