Announcement Announcement Module
Collapse
No announcement yet.
NullPointerException accurs while trying to call service layer from controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • NullPointerException accurs while trying to call service layer from controller

    I'm working with Spring STS, and creating a spring template project.
    (Also with Eclipse-juno, spring 3.1).

    My welcome page of the application is a simple form:

    Code:
          <div id="editPresPage">
             <form action="editPresPage.do" method="post"> 
    		<label>Enter Page Text</label><input type="text" name="page_text"/><br>
    	    <input type="submit" value="Add New Page"/>
    	 </form>		   	  
          </div>
    When i deliver the form ,My controller comes into place:

    Code:
    @Controller
    @RequestMapping(value = "/")
    public class HomeController {
    		
    	private Page_manager_service page_manager_service;
    	
    	public void setPage_manager_service(Page_manager_service page_manager_service) {
    		this.page_manager_service = page_manager_service;
    	}
    		
    	@RequestMapping(value="/editPresPage",method = RequestMethod.POST)
    	public ModelAndView EditPresPage()	  {
    		page_manager_service.check();
    		
    		return new ModelAndView("thanks");		
    	}
    The controller needs to perform the check method (which is marked) from My Service Layer interface:

    Code:
    public interface Page_manager_service {
    	public void check();
    }
    The Service Layer implementation:

    Code:
    public class Page_manager_service_mock_Impl implements Page_manager_service {
    
    	public void check() {
    		System.out.println("check method was done!!!");
    	}
    }
    But i'll getting the following error while executing the check method:

    java.lang.NullPointerException
    at my.topLevel.pack.HomeController.EditPresPage(HomeC ontroller.java:64)



    Here is my servlet-context.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/mvc"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:beans="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
    	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring  beans.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    	
    	<!-- Enables the Spring MVC @Controller programming model -->
    	<annotation-driven />
    
    	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    	<resources mapping="/resources/**" location="/resources/" />
    
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<beans:property name="prefix" value="/WEB-INF/views/" />
    		<beans:property name="suffix" value=".jsp" />
    	</beans:bean>
    	
    	<context:component-scan base-package="my.topLevel.pack" />
    
    </beans:beans>

    Here is my root-context.xml (you can see that i wired the "page_manager_service" bean):

    Code:
    <?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:jee="http://www.springframework.org/schema/jee"
    	xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
    		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">	
    	
    	<!-- Root Context: defines shared resources visible to all other web components -->
    	<context:annotation-config/> 
    	
    	<!--  Service Beans -->    
        <bean id="page_manager_service"  class="my.topLevel.pack.Services.Page_manager_service_mock_Impl">
    	</bean>	
    </beans>
    I did not change anything in the web.xml file.

    This is how my project looks like - you can see that the service layer implementation is being injected because there is an "s" on it:

    Attachment

    I Don't know why i'm getting this error..
    Attached Files

  • #2

    I Don't know why i'm getting this error..
    How should spring now it should inject the field?! You have a controller, you have service but no configuration or hint for spring that the service must be injected. (I suggest a read of the reference guide). The quick answer add @Autowired to the field.

    Comment


    • #3
      Thanks martin, I thought that the "s" sign means that it was injected.

      The problem was solved by doing this steps:

      First step:Adding @Autowired to the page_manager_service field (It's ok to delete the setter).

      Second step: In the root-context.xml file changing:

      <context:annotation-config/>

      to:

      <context:component-scan
      base-package="my.topLevel.pack">
      </context:component-scan>

      Third Step: Removing:

      <bean id="page_manager_service" class="my.topLevel.pack.Services.Page_manager_serv ice_mock_Impl">

      from root-context.xml.

      Fourth Step:Adding @Component to Page_manager_service_mock_Impl

      Comment

      Working...
      X