Spring bean injecting successfully on start up, but when using giving null pointer exception

StackOverflow https://stackoverflow.com/questions/20834795

  •  22-09-2022
  •  | 
  •  

Question

i'm creating a web-app with Spring 3.0.6 Release

My problem is when starting up the server, beans are injecting successfully.....but when using giving Null pointer Exception.

Below is my applicationContext.xml

    <?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:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jd="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/jdbc
   http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">

   <context:annotation-config />
   <context:component-scan base-package="com.icis.sample.web" />
   <mvc:annotation-driven />
   <bean id="dataSource"   class =   "org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />        
   <property name="url" value="jdbc:sqlserver://localhost:3306;DatabaseName=SAMPLESPRING"     />
    <property name="username" value="user1" />        
    <property name="password" value="user1" />
    </bean>

    <bean id="sessionFactory"   class=  "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>

    <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>

            </props>
        </property>

    </bean>
  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="modelDAO" class="com.sample.dao.ModelDAO">
         <property name="hibernateTemplate" ref="hibernateTemplate" /> 
    </bean>

    <bean id="modelService" class="com.sample.service.ModelService">
         <property name="modelDAO" ref="modelDAO" /> 
    </bean>
        <bean id="modelController" class="com.sample.GetAllDataController.ModelService">
         <property name="modelService" ref="modelService" /> 
    </bean>

</beans>

Controller GetAllDataController.java

package com.sample.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.icis.sample.model.FormModel;
import com.icis.sample.service.ModelService;

@Controller
public class GetAllDataController{
private ModelService modelService;
public void setModelService(){
   this.modelService =modelService ;
}
    @RequestMapping("/home")
    public ModelAndView home(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {

        ModelAndView mav = new ModelAndView("home");
        FormModel model = new FormModel();

        modelService.getPort();


         return mav;
    }

}

Please help me, Why injected bean object is null while calling getPort() method?

Was it helpful?

Solution

You need to autowire in the beans

@Autowired
private ModelService modelService;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top