This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Service not injecting in the Controller

Jun 29th, 2011, 09:02 AM

Hi to all! I'm new to spring and i find the same problem always i try to do something. The problem is as follows: in my @Controller bean, spring doesn't inject the @Resorce referencing to the @Service, so when i try to load data from service to an UIData component which is in the controller, the ide shows me a NullPointerException.

I use anotations as you see, and the application is a JSF 2 over Spring 3 and Hibernate 3 too.

Here's the Stack trace of the error:

Code:

org.apache.jasper.el.JspELException: /WEB-INF/jsp/index.jsp(17,12) '#{listarBean.tabla}' Error reading 'tabla' en el tipo paquete.ListarBean
at org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:107)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:178)
at javax.faces.component.UIData.getValue(UIData.java:554)
at javax.faces.component.UIData.getDataModel(UIData.java:1248)
at javax.faces.component.UIData.setRowIndex(UIData.java:447)
at com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:81)
at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:823)
at javax.faces.component.UIData.encodeBegin(UIData.java:937)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1611)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
at com.sun.faces.application.view.JspViewHandlingStrategy.doRenderView(JspViewHandlingStrategy.java:420)
at com.sun.faces.application.view.JspViewHandlingStrategy.renderView(JspViewHandlingStrategy.java:209)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.NullPointerException
at paquete.ListarBean.getTabla(ListarBean.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:62)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
at org.apache.el.parser.AstValue.getValue(AstValue.java:123)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
at org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:101)
... 32 more

Observations
1: a DAO must use @Repository, Business classes (BO/Service) use @Service
2: Is illogical use @Resource and @Autowired both in the same time!, delete @Resource
3 Be sure HibDao is an interface and not a class, since Spring use Proxy pattern
4: create setters and getter with the IDE tools, not a hand

Again
1: Is illogical use @Resource and @Autowired both in the same time!, delete @Resource
3 You must use the interface used for ServicioDao and not use the class name directly, since Spring use Proxy pattern
3: create setters and getter with the IDE tools, not a hand

Seems You are doing a wrong integration about Spring MVC + JSF

@Controller("listarBean") is used for URL resolution and you are using to access your backed bean

Comment

Hi dr_pompeii! Thank for your advices, i know i must have had a lot of errors, even problems with concept, but i don't have more time to learn slowly for this time, i have to present a program this friday to demonstrate i can integrate jsf, spring and hibernate, and spring is giving me a lot of problems as you can see xD, although after i've done and showed this program i'll do, i promise ^^

I've changed the things you've said to me and i edited my first post to show this changes. I only put in this one the things i can't put in the other (i exceed the 10,000 characters).

Now the package "Catalogo" is called "paquete" (the name change is because i had problems with capitalization between "Catalogo" and "catalogo")

I've removed all @Resource annotations and let only the @Autowired ones.

Initially i was accesing my backed bean using the class name, i mean, #{ListarBean.tabla}, and i put things like @Controller only, not @Controller("listarBean"), and the same for @Service and the others, but it didn't work so i tried to use my own names because it could be a problem with spring auto generated names for beans. It worked fine.

In fact i've defined String variable with a fixxed value in the controller, and i can show its value in the jsp using #{listarBean.variable}. Anyways, i can change it if you advice this to me.

Comment

I have one question, my class ServicioDao must be serializable? ListarBean is Serializable and his attribute of type Servicio must be too to get this capability, but this means that i'll have to make HibDao class Serializable too for the same reason, ServicioDao has an attribute of type HibDao (the interface HibDaoInt, not the class implementing it), and over and over again, so it's a bit confusing, isn't it? Anyways, in others examples i've seen no-one defines theese classes as Serializable but the bean one, so i didn't.

Comment

Hi! i have gone on trying and reading to solve this, and i've found this error in the Apache log:

Code:

30-jun-2011 13:16:05 org.apache.catalina.session.StandardSession writeObject
ADVERTENCIA: No puedo serializar atributo de sesión listarBean para sesión 82A791064D24733FC0476E7AB704FCA9
java.io.NotSerializableException: paquete.ServicioDao
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
at org.apache.catalina.session.StandardSession.writeObject(StandardSession.java:1546)
at org.apache.catalina.session.StandardSession.writeObjectData(StandardSession.java:989)
at org.apache.catalina.session.StandardManager.doUnload(StandardManager.java:517)
at org.apache.catalina.session.StandardManager.unload(StandardManager.java:463)
at org.apache.catalina.session.StandardManager.stop(StandardManager.java:667)
at org.apache.catalina.core.StandardContext.stop(StandardContext.java:4611)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3222)
at org.apache.catalina.manager.ManagerServlet.reload(ManagerServlet.java:943)
at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:361)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:199)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:558)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
at java.lang.Thread.run(Thread.java:662)

I've read this happens because i'm using a non serializable object in a persistent session, and that i can ignore it beacuse this is happening when it tries to write to a file session information, but doesn't affect to my program and doesn't cause the null pointer fail.

Comment

I've tried another way, following the spring manual and the intergrating with other web frameworks section. So instead of using the @Controller annotation, i changed it by a @ManagedBean one (as i was definig between <managed-bean> tag in faces-config.xml), using @ManagedProperty(value="#{servicioDao}") (the varaible servicio now is called servicioDao, as the object that represents but without the first capital character) for the service object. Have i to let the setter with @Autowired annotation or must i remove it? Anyways, i still get the same null pointer exception.