【问题标题】:nullpointer exception when querying database查询数据库时出现空指针异常
【发布时间】:2013-04-20 17:15:43
【问题描述】:

我正在尝试从数据库中查询它的休眠项目

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

【问题讨论】:

  • 向我们展示堆栈跟踪
  • 您的sessionFactoryNULL,这意味着它不是正确的Autowired
  • @midhunjose Autowired 意味着 spring 应该在它的 xml 文件中查找 SessionFactory 的实例 - 它没有找到这样的东西,因此你得到一个空值。

标签: java database hibernate session


【解决方案1】:

SessionFactory 为 null - 由于某种原因,它没有被 spring 自动装配。 FeedInputParamsDaoImpl 类上有注释吗?

【讨论】:

    猜你喜欢
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    相关资源
    最近更新 更多