Announcement Announcement Module
Collapse
No announcement yet.
NullPointerException on call to ApplicationContext.getBean() Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • NullPointerException on call to ApplicationContext.getBean()

    Hello:

    I am building an Axis2 Web Service that uses the Spring Framework for managing Hibernate Annotations and the DAO Objects for database manipulation. I am running this Web Service within Axis2 running within Tomcat 5.5.25. I am developing within Eclipse and everything runs fine when running my service in Tomcat within Eclipse. Can someone please provide any information regarding why I am seeing this behavior running my service in Tomcat outside Eclipse?

    Here is my Java source code snippet:
    Code:
    //get the BeanFactory from the applicationContext.xml resource
    ApplicationContext beanFact = ApplicationContextHolder.getContext();
    System.out.println("got ApplicationContext...");
    			
    //create a DAO object for a JumpSet
    JSJumpSetDAO jSetDAO = (JSJumpSetDAO)beanFact.getBean  
                                        ("JSJumpSetDAO");
    System.out.println("got DAO bean for JSJumpSet...");
    Here is my applicationContext.xml snippet:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE ...>
    <beans>
    	   					   
    <bean id="appContextHolder" 
         class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
    
    <!-- ******************************DAOs****************************** -->
    <bean id="JSJumpSetDAO"
         class="com.XXX.jumpstory.datamodeldao.JSJumpSetDAO">
         <property name="sessionFactory">
         <ref bean="sessionFactory" />
         </property>
    </bean>
    Here is my web.xml snippet:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app>
    <display-name>JumpStoryService</display-name>	
    	<listener>
    		<listener-class>
    		org.springframework.web.context.ContextLoaderListener
    		</listener-class>
    	</listener>
    	<!-- Not sure if this is needed
    	<context-param>
    	   <param-name>contextConfigLocation</param-name>
    	   <param-value>classpath:applicationContext.xml</param-value>
    	</context-param>
    	-->
    The "got ApplicationContext..." message gets printed so it appears that my applicationContext is getting loaded, but the NullPointerException is thrown before the "got DAO bean..." message get printed. Please let me know if there is more information needed to dissect this problem.

    Sincerely,
    Mike Sucena
    Software Engineer

  • #2
    can u give the exception stacktrace?

    Comment


    • #3
      How do you actually construct the context? I see that you use custom class for retrieving it - ApplicationContextHolder.

      Comment


      • #4
        Thanks

        Thank you for your replies.

        I have found out that the NullPointerException is because the call to ApplicationContext beanFact = ApplicationContextHolder.getContext() is returning a NULL. The ApplicationContextHolder is not a class of mine. It is from an import org.apache.axis2.extensions.spring.receivers.Appli cationContextHolder. Can you provide any additional information? Is there any more information that I can provide? Thank you for your continued assistance with this issue.

        Sincerely,
        Mike

        Comment


        • #5
          I've never worked with Axis, but little googling shows that you need to specify context listener as well.

          Comment


          • #6
            New error message

            I am now getting the error: Axis2 Can't find Spring's ApplicationContext

            Please provide some guidance. Thank you.

            I am now going to include my services.xml:
            Code:
            <?xml version="1.0" encoding="UTF-8"?>
            <!-- This file was auto-generated from WSDL -->
            <!-- by the Apache Axis2 version: 1.3  Built on : Aug 10, 2007 (04:45:47 LKT) -->
            <serviceGroup>
            
            	<!--
            	<service name="SpringAwareService" class="com.XXX.jumpstory.spring.SpringInit">
            		<parameter name="ServiceClass">com.XXX.jumpstory.spring.SpringInit</parameter>
            		<parameter name="ServiceTCCL">composite</parameter>
            		<parameter name="load-on-startup">true</parameter>
            		<parameter name="ServiceObjectSupplier">
            			org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
            			org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
            		</parameter>
            		<parameter name="SpringBeanName">springAwareService</parameter>
            		<operation name="getValue">
            			<messageReceiver
            				class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />
            		</operation>
            	</service>
            	-->
            	
            	<!--
            	<service name="JumpStoryService" class="com.XXX.jumpstory.spring.SpringInit">
            	-->
            	<service name="JumpStoryService">
            		<messageReceivers>
            			<messageReceiver mep=URL
            				class="com.XXX.jumpstoryservice.JumpStoryServiceMessageReceiverInOut" />
            		</messageReceivers>
            	
            		<!-- Needed parameters to make Axis2 Spring Aware -->
            		<parameter name="ServiceTCCL">composite</parameter>
            		<parameter name="ServiceObjectSupplier">
            			<!--org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier-->
            			org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
            		</parameter>
            		<parameter name="SpringBeanName">JumpStoryService</parameter>
            		
            		<parameter name="ServiceClass">
            			com.XXX.jumpstoryservice.JumpStoryServiceSkeleton
            		</parameter>
            		<parameter name="useOriginalwsdl">true</parameter>
            		<parameter name="modifyUserWSDLPortAddress">true</parameter>
            		<operation name="Logout"
            			mep=URL>
            			<actionMapping>
            				URL/doLogout
            			</actionMapping>
            			<outputActionMapping>
            				URL/JumpStoryService/LogoutResponse
            			</outputActionMapping>
            		</operation>
            		<operation name="Login"
            			mep=URL>
            			<actionMapping>
            				URL/doLogin
            			</actionMapping>
            			<outputActionMapping>
            				URL/JumpStoryService/LoginResponse
            			</outputActionMapping>
            		</operation>
            		<operation name="RequestLicense"
            			mep=URL>
            			<actionMapping>
            				URL/doRequestLicense
            			</actionMapping>
            			<outputActionMapping>
            				URL/JumpStoryService/RequestLicenseResponse
            			</outputActionMapping>
            		</operation>
            		<operation name="Tickle"
            			mep=URL>
            			<actionMapping>
            				URL/doTickle
            			</actionMapping>
            			<outputActionMapping>
            				URL/JumpStoryService/TickleResponse
            			</outputActionMapping>
            		</operation>
            		<operation name="Execute"
            			mep=URL>
            			<actionMapping>
            				URL/doExecute
            			</actionMapping>
            			<outputActionMapping>
            				URL/JumpStoryService/ExecuteResponse
            			</outputActionMapping>
            		</operation>
            	</service>
            </serviceGroup>
            I have also added this to my applicationContext.xml:
            Code:
            	<bean id="JumpStoryService" class="com.XXX.jumpstoryservice.JumpStoryServiceSkeleton">
            	</bean>
            -Mike
            Last edited by msucena; Oct 20th, 2008, 03:22 PM.

            Comment


            • #7
              Originally posted by msucena View Post
              I am now getting the error: Axis2 Can't find Spring's ApplicationContext

              Please provide some guidance. Thank you.

              ...
              Why don't you want to that yourself? Google is happy to provide the answers. Axis documentation is quite clear about Spring integration too.

              Hint: you're trying to mix different concepts at the mentioned example - axis & spring with and without ServletContext.

              Comment


              • #8
                Still having the Axis2 Can't find Spring's ApplicationContext problem

                I did notice that I had mixed some elements of all the approaches described in the Axis2 Spring tutorial.

                I went back to my services.xml & applicationContext.xml and laid them out following the "With a Servlet Context" example, since I am running Axis2 1.3 within Tomcat 5.5.25. I have also added the "SpringInit.java" class following the "Spring inside the AAR" example. I have also added all the Spring, Hibernate, and supporting .jars inside <Tomcat>\webapps\axis2\WEB-INF\lib, I am not sure why I had to do this? I also added my applicationContext.xml and MySQL.properties files to <Tomcat>\webapps\axis2\WEB-INF. I now notice in my Tomcat's stdout log that when starting Tomcat all the Spring and Hibernate [INFO] statements get displayed and it appears my beans do get initialized; however, my first call to my Web Service, a Login, which actually has nothing to do with any beans, fails with the "Axis2 Can't find Spring's ApplicationContext" error still.

                Can someone provide any new information why I am still seeing this behavior? Thank you for your continued assistance with this issue.

                Here is the SpringInit.java class I have added:
                Code:
                package com.XXX.jumpstory.spring;
                
                import org.apache.axis2.context.ConfigurationContext;
                import org.apache.axis2.context.MessageContext;
                import org.apache.axis2.description.AxisService;
                import org.apache.axis2.engine.ServiceLifeCycle;
                
                import org.springframework.context.ApplicationContext;
                import org.springframework.context.support.ClassPathXmlApplicationContext;
                import org.springframework.web.context.support.WebApplicationContextUtils;
                
                public class SpringInit implements ServiceLifeCycle
                {
                    /**
                     * This will be called during the deployment time of the service. 
                     * irrespective of the service scope this method will be called.
                     * 
                     */
                	public void startUp(ConfigurationContext ignore, AxisService service)
                	{
                	 try
                		{
                            System.out.println("Starting spring init");
                            
                            ClassLoader classLoader = service.getClassLoader();
                            System.out.println("got classLoader...");
                            ClassPathXmlApplicationContext appCtx = new
                            	ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"}, false);
                            //ApplicationContext appCtx = 
                            //	WebApplicationContextUtils.getWebApplicationContext(MessageContext.getCurrentMessageContext().getServiceContext().);
                            System.out.println("got appCtx...");
                            appCtx.setClassLoader(classLoader);
                            System.out.println("set appCtx's classLoader...");
                            appCtx.refresh();
                            System.out.println("refreshed appCtx...");            
                            System.out.println("spring loaded");
                            
                        }
                	catch (Exception ex)
                		{
                            ex.printStackTrace();
                            System.out.println("Exception during SpringInit.startUp:  " + ex.getMessage());
                        }
                	}
                
                    /**
                     * This will be called during the system shut down time. 
                     * irrespective of the service scope this method will be called
                     * 
                     */
                	public void shutDown(ConfigurationContext ignore, AxisService service)
                	{
                		// empty implementation
                	}
                }
                I have added this to my services.xml:
                Code:
                <service name="JumpStoryService" class="com.XXX.jumpstory.spring.SpringInit">
                Here is a snippet from my Tomcat's stdout...log when starting Tomcat after deploying my .aar:
                Code:
                Starting spring init
                got classLoader...
                got appCtx...
                set appCtx's classLoader...
                [INFO] Refreshing org.springframework.context.support.Clas[email protected]: display name [org[email protected]15dd910]; startup date [Tue Oct 21 17:01:25 EDT 2008]; root of context hierarchy
                [INFO] Loading XML bean definitions from class path resource [applicationContext.xml]
                [INFO] Bean factory for application context [org[email protected]15dd910]: org.s[email protected]14d5bc9
                [INFO] Loading properties file from class path resource [MySQL.properties]
                [INFO] Pre-instantiating singletons in org.s[email protected]14d5bc9: defining beans [JumpStoryService,JSJumpSetDAO,JSJumpSegmentDAO,JSBoxElementDAO,JSJumpSetEditorialLinkDAO,JSJumpSegmentEditorialLinkDAO,transactionManager,sessionFactory,classList,dsPropertyConfigurer,dataSource]; root of factory hierarchy
                [INFO] Loading properties file from class path resource [MySQL.properties]
                [INFO] Hibernate Annotations 3.3.0.GA
                [INFO] Hibernate 3.2.5
                [INFO] hibernate.properties not found
                [INFO] Bytecode provider name : cglib
                [INFO] using JDK 1.4 java.sql.Timestamp handling
                [INFO] Building new Hibernate SessionFactory
                [INFO] Binding entity from annotated class: com.maned.jumpstory.datamodel.JSJumpSet
                [INFO] Bind entity com.maned.jumpstory.datamodel.JSJumpSet on table JS_JUMPSET
                [INFO] Binding entity from annotated class: com.maned.jumpstory.datamodel.JSJumpSegment
                [INFO] Bind entity com.maned.jumpstory.datamodel.JSJumpSegment on table JS_JUMPSEGMENT
                [INFO] Binding entity from annotated class: com.maned.jumpstory.datamodel.JSBoxElement
                [INFO] Bind entity com.maned.jumpstory.datamodel.JSBoxElement on table JS_BOXELEMENT
                [INFO] Binding entity from annotated class: com.maned.jumpstory.datamodel.JSJumpSetEditorialLink
                [INFO] Bind entity com.maned.jumpstory.datamodel.JSJumpSetEditorialLink on table JS_JUMPSET_EDITORIALLINK
                [INFO] Binding entity from annotated class: com.maned.jumpstory.datamodel.JSJumpSegmentEditorialLink
                [INFO] Bind entity com.maned.jumpstory.datamodel.JSJumpSegmentEditorialLink on table JS_JUMPSEGMENT_EDITORIALLINK
                [INFO] Mapping collection: com.maned.jumpstory.datamodel.JSJumpSet.jumpSetChildren -> JS_JUMPSEGMENT
                [INFO] Mapping collection: com.maned.jumpstory.datamodel.JSJumpSegment.jumpSegmentChildren -> JS_BOXELEMENT
                [INFO] Hibernate Validator 3.0.0.GA
                [INFO] Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider
                [INFO] RDBMS: MySQL, version: 5.0.45-community-nt
                [INFO] JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.0.5 ( $Date: 2007-03-01 00:01:06 +0100 (Thu, 01 Mar 2007) $, $Revision: 6329 $ )
                [INFO] Using dialect: org.hibernate.dialect.MySQLDialect
                [INFO] Using default transaction strategy (direct JDBC transactions)
                [INFO] No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
                [INFO] Automatic flush during beforeCompletion(): disabled
                [INFO] Automatic session close at end of transaction: disabled
                [INFO] JDBC batch size: 15
                [INFO] JDBC batch updates for versioned data: disabled
                [INFO] Scrollable result sets: enabled
                [INFO] JDBC3 getGeneratedKeys(): enabled
                [INFO] Connection release mode: on_close
                [INFO] Maximum outer join fetch depth: 2
                [INFO] Default batch fetch size: 1
                [INFO] Generate SQL with comments: disabled
                [INFO] Order SQL updates by primary key: disabled
                [INFO] Order SQL inserts for batching: disabled
                [INFO] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
                [INFO] Using ASTQueryTranslatorFactory
                [INFO] Query language substitutions: {}
                [INFO] JPA-QL strict compliance: disabled
                [INFO] Second-level cache: enabled
                [INFO] Query cache: disabled
                [INFO] Cache provider: org.hibernate.cache.NoCacheProvider
                [INFO] Optimize cache for minimal puts: disabled
                [INFO] Structured second-level cache entries: disabled
                [INFO] Echoing all SQL to stdout
                [INFO] Statistics: disabled
                [INFO] Deleted entity synthetic identifier rollback: disabled
                [INFO] Default entity-mode: pojo
                [INFO] Named query checking : enabled
                [INFO] building session factory
                [INFO] Not binding factory to JNDI, no JNDI name configured
                [INFO] Using DataSource [[email protected]] of Hibernate SessionFactory for HibernateTransactionManager
                refreshed appCtx...
                spring loaded

                Comment


                • #9
                  Possible JNDI setting issue related to Spring integration.

                  I appear to have Spring & Hibernate now functioning correctly with my Axis2 Web Service; however, because of the following issue I can not completely prove or disprove that, but it appears I now have a new JNDI problem that may or may not be related to Spring integration. My Web Service cannot find my JNDI settings in my Tomcat's server.xml file.

                  Is this message, from my Tomcat log, Not binding factory to JNDI, no JNDI name configured, a problem with my Spring and Axis2 setup. I am trying to access JNDI settings from my Tomcat's server.xml file and I am not sure if Spring not configuring a JNDI name is what is preventing me from finding my JNDI settings. Please let me know if this is a problem, and if so, please let me know how to resolve this. Am I missing a some JNDI JAR? Thanks for your continued assistance.

                  Sincerely,
                  Mike

                  Comment


                  • #10
                    It works!

                    My Axis2 Web Service now appears to be working completely with Spring and Hibernate. I had to add beans to the applicationContext.xml for my JNDI settings and issue a ApplicationContext.getBean() to get the values of my JNDI settings. Thank you for all of your assistance with resolving this issue. I greatly appreciate it.

                    Sincerely,
                    Mike

                    Comment

                    Working...
                    X