Query query =session.createSQLQuery(hql)查询时出现空指针异常

代码:

  public Book findByBookNo(String bookNo) {
        // TODO Auto-generated method stub
        Session session = HibernateSessionFactory.getSession();
        String hql="from Book as book where book.bookNo=:bookNo";
        Query query =session.createSQLQuery(hql);  //进行到这步就开始出错了
        query.setString("bookNo", bookNo);
        List<Book> books = query.list();
        HibernateSessionFactory.closeSession();
        if(books.size()>0){
            return books.get(0);
        }
        else{
            return null;
        }
    }

出现空指针异常异常如下:

  java.lang.NullPointerException
    com.Dao.BookDAOImpl.findByBookNo(BookDAOImpl.java:29)
    com.Service.BookServiceImpl.inputBook(BookServiceImpl.java:14)
    com.Action.BookInputAction.execute(BookInputAction.java:37)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.56 logs.

3个回答

Query query =session.createSQLQuery(hql); //进行到这步就开始出错了
session的创建方法好像有两个,这里应该是session.createQuery

mingqingchangcheng
mingqingchangcheng 回复miaojian0620: 如果各个方面没有为问题那大概是jdk版本的错误吧,看看2个程序jdk配置一样
接近 5 年之前 回复
mingqingchangcheng
mingqingchangcheng 回复miaojian0620: 如果各个方面没有为问题那大概是jdk版本的错误吧,看看2个程序jdk配置一样
接近 5 年之前 回复
miaojian0620
miaojian0620 我在另一个程序这样写是可以查询的,这个不知道哪里错了,语法什么的都是正常的
接近 5 年之前 回复
mingqingchangcheng
mingqingchangcheng as就不用去写,直接在后面写要用的变量名
接近 5 年之前 回复
mingqingchangcheng
mingqingchangcheng as就不用去写,直接在后面写要用的变量名
接近 5 年之前 回复
mingqingchangcheng
mingqingchangcheng 第二点:看你的语句在数据库中的查询是否成功,因为不同数据库中不同,oracle
接近 5 年之前 回复
mingqingchangcheng
mingqingchangcheng 回复miaojian0620: 第一点:hql语句里面全是类名和属性;
接近 5 年之前 回复
miaojian0620
miaojian0620 改成createQuery也是那样出错,我在另外一个练习里就不会出错,这是怎么回事?
接近 5 年之前 回复

HQL问题吧。是不是这样 "...book.bookNo=:"+bookNo;,

miaojian0620
miaojian0620 不是,就是bookNo是个变量,不知道为什么会出这个问题可能是配置什么的出错,语法什么都没错
接近 5 年之前 回复
miaojian0620
miaojian0620 不是这个原因
接近 5 年之前 回复

List books = query.list(); 没查出数据出来,books为null了吧

enson16855
Mr-稻帅 回复miaojian0620: session为null了吧
接近 5 年之前 回复
miaojian0620
miaojian0620 不是这个问题,而是在 Query query =session.createSQLQuery(hql);这句出问题,而且莫名其妙的出问题。
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐