hibernate 中的java.lang.NullPointerException
BookDAOImpl.java .....
public String getMaxID() {
String sql = "SELECT MAX(bookId)FROM Books";
List list =this.getHibernateTemplate().find(sql);
Iterator itr = list.iterator();
while(itr.hasNext()){
Books book =(Books)itr.next();
System.out.println(book.getBookId());
}
return null;
}
.....
BookTest.java
public class BookTest {
public static void main(String[] args) {
BookDAOImpl book = new BookDAOImpl();
book.getMaxID();
}
}
用的是hibernate跟spring
运行之后就出现了
Exception in thread "main" java.lang.NullPointerException
at org.fcb.dao.BookDAOImpl.getMaxID(BookDAOImpl.java:58)
at org.fcb.dao.BookTest.main(BookTest.java:18)
一直没找到解决的方法。
第58行是
List list =this.getHibernateTemplate().find(sql);
session由spring管理与实例化吧
搜索更多相关主题的帖子:
java hibernate lang itr book
----------------解决方案--------------------------------------------------------
有东西没配好,一般Hibernate出现空指针异常都是这个原因,再检查下
----------------解决方案--------------------------------------------------------
哥们你是不是返回的null
public String getMaxID() {
String sql = "SELECT MAX(bookId)FROM Books";
List list =this.getHibernateTemplate().find(sql);
Iterator itr = list.iterator();
while(itr.hasNext()){
Books book =(Books)itr.next();
System.out.println(book.getBookId());
}
return null;
}
----------------解决方案--------------------------------------------------------
new HibernateTemplate(sessionFactory);
再错就检查下配置文件有没有注入,
----------------解决方案--------------------------------------------------------