facebook

Error executing find method of Spring Scaffold Struts Action

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

    ssquire
    Member

    When I execute Spring Scaffolded ‘find’ methods (ie. findall…, or findby…) from my Struts generated Action, I get the following error in my browser:

    HTTP Status 500 –

    ——————————————————————————–

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    javax.servlet.ServletException: java.lang.NullPointerException
    org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
    com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

    root cause

    java.lang.NullPointerException
    com.yourcompany.struts.action.UserLoginAction.execute(UserLoginAction.java:79)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
    com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.

    ——————————————————————————–

    Apache Tomcat/6.0.13

    The root cause says its from “UserLoginAction.java: line79”. Where line 79 is the call to the find all method (see attached listing with Red highlight of line 79). I also attached the file where findAllUserlogintbls resides (UserlogintblDAOImpl.java).

    Attachments:
    You must be logged in to view attached files.
    #320945 Reply

    ssquire
    Member

    I have also attached the log from my session.

    Attachments:
    You must be logged in to view attached files.
    #320959 Reply

    cconway
    Member

    You can step through this code in the debugger by running your server in Debug mode and setting a breakpoint on line 79 of your action. If you run this in the debugger I’m pretty sure you’ll see that the item that is null is “uNamePtr”. I don’t see in your file where you ever set uNamePtr to anything, so I would expect it to be null.

    In the Struts/Spring tutorial from the last post, I think they were using the constructor of the Action to initialize a similar variable.

    #320988 Reply

    ssquire
    Member

    Cindy,
    I made the changes you identified and I still seem to be having a ‘Null Pointer’ problem somewhere in Abstract Entity Manager when CreateNamedQuery is called. It almost looks like a problem in the Hash Code algorithm (please forgive my speculation if it’s wrong – I’m definitely not an expert :-).

    My browser yields the following error:

    HTTP Status 500 –

    ——————————————————————————–

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    javax.servlet.ServletException: java.lang.NullPointerException
    org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
    com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

    root cause

    java.lang.NullPointerException
    org.skyway.spring.util.dao.AbstractJpaDao.createNamedQuery(AbstractJpaDao.java:214)
    org.skyway.spring.util.dao.AbstractJpaDao.createNamedQuery(AbstractJpaDao.java:205)
    org.userlogintblapp.dao.UserlogintblDAOImpl.findAllUserlogintbls(UserlogintblDAOImpl.java:87)
    com.yourcompany.struts.action.UserLoginAction.execute(UserLoginAction.java:79)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
    com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.

    ——————————————————————————–

    Apache Tomcat/6.0.13.

    I have also attached a Stack Trace file from when the error occurred, my changed UserLoginAction.java file, and my Console Error Log.

    Attachments:
    You must be logged in to view attached files.
    #321000 Reply

    cconway
    Member

    It looks like the NullPointerException occurs because the EntityManager is not being properly injected into the UserlogintblDAOImpl class.

    In your document titled “Stack – Abstract Entity Manager Error.doc”. This first line on the stack indicates that AbstractEntityManagerFactoryBean had some problem invoking a method. But what problem? The stack is incomplete, there should be some sort of message above the stack trace indicating the nature of the problem. Please post the full stack trace including the information about the actual error at that line. This is the line I’m referring to in the stack:

    AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(Object, Method, Object[]) line: 483

    Also, are you using Spring 2.5 or Spring 3.0?

    Thanks,

    #321031 Reply

    ssquire
    Member

    Cindy,
    I’ve attached 2 documents one that identifies my Spring Version (which I believe is 2.5) and a trace from the file org.springframework.orm.jpa.AbstractEntityManagerFactoryBean. If that doesn’t work please let me know. Also, if I need to be using Spring 3.0 please give me update instructions, I’d appreciate it. I’ll also run it again and try to get more stack data.

    Attachments:
    You must be logged in to view attached files.
    #321032 Reply

    ssquire
    Member

    OK Cindy,
    After further analysis it seems to be failing on the second call to getEntityManager. I don’t know why because the parameters in the call look the same. I have attached several docs to help your analysis (hopefully).

    Attachments:
    You must be logged in to view attached files.
    #321161 Reply

    ssquire
    Member

    Cindy,
    I also noticed that in addition to my Spring Version being 2.5, my Struts Version is 1.2. How do I update these respectively?

    #321190 Reply

    cconway
    Member

    Hi ssquire,

    I’m sorry but these documents aren’t providing any useful information. Please clean your server log, then start your server and get the error. Then attach that entire server log as a text file and I’ll be happy to take a look.

    As far as upgrading Spring and Struts, here is a guide for Spring: http://static.springsource.org/spring/docs/upgrade/spring3/html/

    For struts, there seems to be online resources to help you understand the steps necessary.

    You may want to consider scaffolding your back-end in a new project remembering to select Spring 3.0 on the scaffolding wizard (it should be the default). The scaffolding wizard will add the necessary libraries to your project, just follow the instructions on the wizard. Then you’ll have to re-apply your struts changes to that project, maybe selecting the desired version of struts libraries.

    #321235 Reply

    ssquire
    Member

    Here is a copy of the server log file, Cindy.

    Attachments:
    You must be logged in to view attached files.
    #321241 Reply

    ssquire
    Member

    Cindy,
    I did actually find something interesting that may be an issue. I rebuilt a web project several times and discovered that if I follow this order I always end up with a ‘log4j’ error:
    1. Create Web Project.
    2. Scaffold Spring CRUD with the MySQL DB that I created.
    3. Add Struts capabilities beyond Struts 1.2 (such as 1.3 or 2.0).
    4. Run and receive ‘log4’ error.

    #321286 Reply

    cconway
    Member

    Hi ssquire,

    Unfortunately my remote debugging skills aren’t helping you through this problem. If you like, you can send me an export of your project and I can attempt to figure it out in a more hands-on mode.

    To send the project, click on my name (at the left side of this message) to go to my profile, there you should be able to send me a private message where you can attach the project.

    Also, there is a log4j error that complains about no appenders having been configured that is benign.

    Thanks,

    #321317 Reply

    cconway
    Member

    Steve,

    Thanks for sending the project. I think I’ve got it figured out!

    The problem is that you can’t just construct a UserlogintblDAOImpl. You need to use an instance of the DAO that has been constructed by Spring and therefore has been fully configured including the autowiring of the entity manager.

    What you need to do is get an instance of the DAO from the application context. There are a few different ways to do that, most of which seem to be deprecated.

    The only way I’ve found so far that doesn’t use deprecated classes is to annotate a class with @Component then implement the ApplicationContextAware interface to get the ApplicationContext injected into the class. The trick is that the applicationContext needs to be static. I don’t know if that’s good or bad but it does work. For example, just working with the classes you have,

    
    // (1)  Make the class a spring bean and implement the ApplicationContextAware interface
    @Component 
    public class UserLoginAction extends Action implements ApplicationContextAware {  
    
        // (2) create a static to be set by the setApplicationContext() method.
        static ApplicationContext applicationContext; 
    
        // (3) This must be the interface, not the impl.  Also, don't construct it here.
        UserlogintblDAO uNamePtr;   
    
        public ActionForward execute(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response) {
            ...
    
            // (4) Get the dao from the context.
            if (uNamePtr == null) {
                uNamePtr = (UserlogintblDAO)applicationContext.getBean("UserlogintblDAO"); 
            }
            
            ptr = uNamePtr.findAllUserlogintbls(1,3);    
            ...
        }
    
        // (5) implement the ApplicationContextAware interface method.
        @Override
        public void setApplicationContext(ApplicationContext context)
                throws BeansException {
            this.applicationContext = context;
            
        }
    
    }
    

    If you don’t want to do this in your class, take a look at this post for an example of using a utility class: http://stackoverflow.com/questions/5835910/how-to-integrate-an-old-struts-application-with-spring-3-x

    Also, because your action is in a different package than your other spring beans, you’ll need to let Spring know about this new @Component class by adding a new component scan entry to the HomeCareSC-web-context.xml like this:

    <context:component-scan base-package="com.yourcompany.struts.action" scoped-proxy="interfaces" />

    There may still be a better way to do this so if you find one, please post it back here for the benefit of others.

    #321321 Reply

    ssquire
    Member

    Thanks Cindy! I obviously have more studying to do.

    #321886 Reply

    ssquire
    Member

    Cindy,
    First of all, THANKS. Your solution worked great! I’ve got a minor problem in that Find All seems to be finding all but my 1st element, but I should be able to figure that one out.

    Also, you told me to publish a better solution if I found one. Well, I don’t think it is a better solution, but it is clearly a solution that has merits (even though I have chosen not to implement it at this time). The link below is from Siegfried Bolz’s blog where he has published an alternative which allows the Spring Application Context to be accessed from anywhere in your program. It seems to have merits, let me know what you think. (I’m far from expert 🙂

    http://blog.jdevelop.eu/2008/07/06/access-the-spring-applicationcontext-from-eve rywhere-in-your-application/

Viewing 15 posts - 1 through 15 (of 16 total)
Reply To: Error executing find method of Spring Scaffold Struts Action

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