Note that there are some explanatory texts on larger screens.

plurals
  1. POException java.lang.NullPointerException accurs while trying to reach the service layer from my controller
    primarykey
    data
    text
    <p>I'm working with Spring STS (Also with Eclipse-juno, spring 3.1), and creating a spring template project.</p> <p>My welcome page of the application is a simple form:</p> <pre><code> &lt;div id="editPresPage"&gt; &lt;form action="editPresPage.do" method="post"&gt; &lt;label&gt;Enter Page Text&lt;/label&gt;&lt;input type="text" name="page_text"/&gt;&lt;br&gt; &lt;input type="submit" value="Add New Page"/&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p>When i deliver the form ,My controller comes into place:</p> <pre><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"); } </code></pre> <p>The controller needs to perform the check method (which is marked) from My Service Layer interface:</p> <pre><code>public interface Page_manager_service { public void check(); } </code></pre> <p>The Service Layer implementation:</p> <pre><code>public class Page_manager_service_mock_Impl implements Page_manager_service { public void check() { System.out.println("check method was done!!!"); } } </code></pre> <p>But i'll getting the following error while executing the check method:</p> <p><em><strong>java.lang.NullPointerException at my.topLevel.pack.HomeController.EditPresPage(HomeController.java:64)</em></strong> </p> <p>Here is my servlet-context.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt; &lt;!-- Enables the Spring MVC @Controller programming model --&gt; &lt;annotation-driven /&gt; &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --&gt; &lt;resources mapping="/resources/**" location="/resources/" /&gt; &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --&gt; &lt;beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt; &lt;beans:property name="suffix" value=".jsp" /&gt; &lt;/beans:bean&gt; &lt;context:component-scan base-package="my.topLevel.pack" /&gt; &lt;/beans:beans&gt; </code></pre> <p>Here is my root-context.xml (you can see that i wired the "page_manager_service" bean):</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;!-- Root Context: defines shared resources visible to all other web components --&gt; &lt;context:annotation-config/&gt; &lt;!-- Service Beans --&gt; &lt;bean id="page_manager_service" class="my.topLevel.pack.Services.Page_manager_service_mock_Impl"&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>I did not change anything in the web.xml file.</p> <p>I can see that the service layer implementation is being injected because there is an "s" on it.</p> <p>I'm able to transfer parameters from the view to the controller- so the problem isn't there.</p> <p>It's in the controller&lt;-> service layer communication.</p> <p>I Don't know why i'm getting this error..</p> <p>The problem was solved by doing this steps:</p> <p>First step:Adding @Autowired to the page_manager_service field (It's ok to delete the setter).</p> <p>Second step: In the root-context.xml file changing:</p> <pre><code>&lt;context:annotation-config/&gt; </code></pre> <p>to:</p> <pre><code>&lt;context:component-scan base-package="my.topLevel.pack"&gt; &lt;/context:component-scan&gt; </code></pre> <p>Third Step: Removing: </p> <pre><code>&lt;bean id="page_manager_service" class="my.topLevel.pack.Services.Page_manager_service_mock_Impl"&gt; </code></pre> <p>from root-context.xml.</p> <p>Fourth Step:Adding @Component to Page_manager_service_mock_Impl</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload