2017-02-28 97 views
0

我DaoClasshibernatetemplate.getSessionFactory()抛出的NullPointerException

@Repository("genObj") 
public class GeneralQueries { 
    HibernateTemplate hibernatetemplate; 
    public HibernateTemplate getHibernatetemplate() { 
     return hibernatetemplate; 
    } 
    public void setHibernatetemplate(HibernateTemplate hibernatetemplate) { 
     this.hibernatetemplate = hibernatetemplate; 
    } 
    public String getStringfromQuery(String sql) 
{ 
    SessionFactory sessionFactory=hibernatetemplate.getSessionFactory(); 
    Session session=sessionFactory.openSession(); 

    String data=null; 
    try 
    { 
     System.out.println(sql); 
     data=session.createSQLQuery(sql).list().toString(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

    return data; 
}} 

此方法返回的数据作为串

我的控制器类

@Controller 
public class SchoolStudentsConfirmationContrl 
{ 
    @Autowired 
    SchoolStudentsConfirmationIntr schoolstdconfirmservice; 
    @Autowired 
    GeneralQueries genObj=new GeneralQueries(); 

    @RequestMapping(value="/getData",method=RequestMethod.GET) 
    public ModelAndView getData(@ModelAttribute("schooldetailsform")SchoolDetailsForm formbean,HttpServletRequest request) 
    { 
     String PageHeading = ""; 


     try 
     { 
     String district = request.getSession().getAttribute("dist_code").toString(); 

     PageHeading = "BAS Students Confirmation for the Academic Year:"+ formbean.getAc_year() + " <br> District:" 
        + genObj.getStringfromQuery("select dist_name from pmss_districts_mst where dist_code=" + district + "")+""; 

     mav.setViewName("showreportwithmenu"); 

     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
     return mav; 
    } 
} 

林试图调用genObj.getStringfromQuery()方法但它会抛出空行指针异常在行 SessionFactory sessionFactory = hibernatetemplate.getSessionFactory();

我的配置文件

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 

    <context:annotation-config /> 
    <context:component-scan base-package="cgg.gov.in.*" annotation-config="true"/> 


    <bean id="tiles" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
     <property name="viewClass"> 
      <value> 
       org.springframework.web.servlet.view.tiles3.TilesView 
      </value> 
     </property> 
    </bean> 

    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"> 
     <property name="definitions"> 
      <list> 
       <value>/WEB-INF/tiles.xml</value> 
      </list> 
     </property> 
    </bean> 

    <bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/jsp/"/> 
    <property name="suffix" value=".jsp"/> 
    </bean> 

    <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.postgresql.Driver" /> 
     <property name="url" value="jdbc:postgresql://x.x.x.x/test" /> 
     <property name="username" value="postgres" /> 
     <property name="password" value="postgres" /> 
    </bean> 

    <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" name="sessionFactory"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
       <prop key="hibernate.show_sql">false</prop> 
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
       <prop key="hibernate.autocommit">false</prop> 
      </props> 
     </property> 
     <property name="annotatedClasses" > 
      <list> 
       <value>cgg.gov.in.model.login.LoginForm</value> 
      </list> 
     </property> 
    </bean> 

     <bean class="org.springframework.orm.hibernate4.HibernateTemplate" name="hibernatetemplate"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
     </bean> 

    <mvc:default-servlet-handler /> 
    <mvc:annotation-driven /> 

</beans> 
+1

您正在执行基本上使自动布线无法正常工作的新GeneralQueries(),请使用spring管理的实例。在你的dao的'HIbernateTemplate'字段中没有'@ Autowired'。另外你的代码有缺陷,除非你想遇到奇怪的问题,否则从来没有使用'openSession'使用'getCurrentSession'('openSession'在需要时打开新会话和'Connection',因为你没有正确关闭,你会得到连接首先是泄漏和内存泄漏)。 –

+2

另外,当你使用'Session'和'SessionFactory'时,我建议停止使用'HibernateTemplate'并直接注入'SessionFactory'。 –

回答

0

你有hibernatetemplate豆定义。但是您还没有说明将GeneralQueries类中的变量hibernatetemplate指向定义的bean。

您可以通过两种方式做到这一点,

1)创建XML为GeneralQueries一个bean,定义如下属性,

<bean name="generalQueries" class="package.GeneralQueries"> 
    <property name="hibernatetemplate" ref="hibernatetemplate" /> 
</bean> 

您已经定义的制定者。 记得从GeneralQueries中删除@Repository,如果您在xml中将其定义为bean。

2) Autowire hibernatetemplateGeneralQueries如下。

@Repository("genObj") 
public class GeneralQueries { 
    @Autowired 
    HibernateTemplate hibernatetemplate; 
    //rest of code 
} 

GeneralQueries必须在组件扫描下。

注意:此外,@ m-deinum建议,从SchoolStudentsConfirmationContrl删除new GeneralQueries()

相关问题