首页 > hql语句提示空指针

hql语句提示空指针

INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: 
    select
        spring001b0_.PRICE as col_0_0_ 
    from
        spring001_book spring001b0_ 
    where
        spring001b0_.BOOKNAME=?

java.lang.NullPointerException
    at crums.BaseCRUMimpl.getPrice(BaseCRUMimpl.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

出问题代码:

@Repository
public class BaseCRUMimpl implements BasicCRUM {

    @Autowired
    private SessionFactory factory;

    public Session getSession() {

        return factory.getCurrentSession();
    }

    @Override
    public int getPrice(String bookName) {

        String hql = "SELECT b.price FROM Spring001Book b WHERE b.bookname=:bookname";

        Query query = getSession().createQuery(hql);

        query.setParameter("bookname", bookName);

        return (Integer) query.uniqueResult();
    }

return (Integer) query.uniqueResult();
这里出现空指针。

...


代码有问题,可能传参错误,比如username和bookname传错位置了...导致索搜不到内容为null。也可以对出现空指针那一句做个判断....

【热门文章】
【热门文章】