facebook

Problem instantiating DAO

  1. MyEclipse IDE
  2.  > 
  3. Spring Development
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #318042 Reply

    Hi all, this is my first time with Spring, so please forgive me if I say anything stupid 😀

    The problem is:
    I’ve scaffolded from various tables in my db, so now I’ve domain package, web package, dao package and service package.

    My java beans have been converted to another type of bean, let’s say webbeans, to accomodate changes in the interfaces without changing the beans, and in my web package (in every EntityController) I’ve setup a converter. Everything works fine until I convert a bean in its own controller, but if I try to instantiate a controller in another controller, when I try to use a dao from the newly instantiated controller I get a nullpointer exception related to the dao I’m trying to use.

    Following an example:
    delidete.domain.DatiGenerali is the bean
    delidete.webdomain.DatiGeneraliWeb is the webbean
    delidete.web.DatiGeneraliController is the controller, where I put my converters public DatiGeneraliWeb convertToWeb(DatiGenerali d) and public DatiGenerali convertToBean(DatiGeneraliWeb d).
    The entity DatiGenerali has a Soggettiindirizzi object, so in my convertToWeb of the class DatiGeneraliController I do:
    temp.setSoggettiindirizzi(new SoggettiindirizziController().convertToBean(d.getSoggettiIndirizzi()));

    In the method convertToBean of SoggettiindirizziControllerclass I call this function:
    web.setSoggetti(soggettiDAO.findSoggettiBySoggettiCodice(s.getSoggettiCodice()).getSoggettiRagSocCognNome());

    but soggettiDAO is null if I call it from DatiGeneraliController, and not null if I call it from the webpage viewSoggetti.jsp

    In the class SoggettiIndirizziController there is this initialization:
    /**
    * DAO injected by Spring that manages Soggetti entities
    *
    */
    @Autowired
    private SoggettiDAO soggettiDAO;

    Stacktrace:

    8-lug-2011 11.46.08 org.apache.catalina.core.StandardWrapperValve invoke
    GRAVE: Servlet.service() for servlet DeliDete Servlet threw exception
    java.lang.NullPointerException
    at delidete.web.SoggettiindirizziController.convertToWeb(SoggettiindirizziController.java:239)
    at delidete.web.SoggettiindirizziController.convertToWeb(SoggettiindirizziController.java:225)
    at delidete.web.DatigeneraliController.convertToWeb(DatigeneraliController.java:1049)
    at delidete.web.SoggettiindirizziController.convertToWeb(SoggettiindirizziController.java:239)
    at delidete.web.SoggettiindirizziController.convertToWeb(SoggettiindirizziController.java:225)
    at delidete.web.SoggettiindirizziController.listSoggettiindirizzis(SoggettiindirizziController.java:459)
    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 org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
    at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    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:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)

    Please if somebody can help me I would be grateful!
    Andrea

    #318230 Reply

    jayperkins
    Member

    Hi Andrea,

    You are getting the null pointer exception because you are directly instantiating the controller. Spring will not perform dependency injection when you create an instance in this manner, so your dao will not be wired in.

    I believe you could auto wire your controllers into each other, or you may want to break the logic out into its own service bean and wire those service beans into the controllers that need them.

    Hope this helps.

    Jay

    #318235 Reply

    Hi Jay, I already found out this by myself a few days ago, but didn’t find the time to post this on the forum.
    But I REALLY thank you for your help!
    Andrea

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: Problem instantiating DAO

You must be logged in to post in the forum log in