【发布时间】: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
【问题讨论】:
-
向我们展示堆栈跟踪
-
您的
sessionFactory是NULL,这意味着它不是正确的Autowired! -
@midhunjose Autowired 意味着 spring 应该在它的 xml 文件中查找 SessionFactory 的实例 - 它没有找到这样的东西,因此你得到一个空值。
标签: java database hibernate session