Announcement Announcement Module
Collapse
No announcement yet.
NullPointerException when bean extends another spring bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • NullPointerException when bean extends another spring bean

    Hi there,

    I'm looking to use Webwork2 along with Spring for a project. I'm using the xwork-spring-integration module and it's all good. All my action classes extend a BaseAction class (as most example apps seem to do) and i'm able to inject dependancies into whatever action is being used but I get a NullPointerException when trying to access the dependancies that I'm injecting into the BaseAction. I'm sure this is a common structure with the Service normally being injected into a BaseObject/Action etc.

    I'm using Tomcat 4.1, btw. Here's the code:

    BaseAction.java
    Code:
    package com.affiliate.actions;
    
    import com.opensymphony.xwork.ActionSupport;
    import com.opensymphony.webwork.ServletActionContext;
    import com.affiliate.bo.UserBo;
    
    public class BaseAction extends ActionSupport{
    
    	public UserBo userBo;
    	public String testAttribute;
    	
    	public String getTestAttribute(){
    		return testAttribute;
    	}
    	public void setTestAttribute(String testAttribute){
    		this.testAttribute = testAttribute;
    	}
    	
    	public void setUserBo(UserBo userBo){
    		this.userBo = userBo;
    	}
    	public UserBo getUserBo(){
    		return userBo;
    	}
    	
    }

    AnAction.java
    Code:
    package com.affiliate.actions;
    
    public class AnAction extends BaseAction{
    	
    	public String attribute;
    	
    	public void setAttribute(String attribute){
    		this.attribute = attribute;
    	}
    	public String getAttribute(){
    		return attribute;
    	}
    	
    	public String execute(){
    		attribute = getUserBo().getFirstName();
    		return SUCCESS;
    	}
    
    }

    Next, the main applicationContext:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
        "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    
      <bean name="baseAction" class="com.affiliate.actions.BaseAction" lazy-init="false">
        <property name="userBo"><ref bean="userBoBean" /></property>
        <property name="testAttribute"><value>Fuckerty</value></property>
      </bean>
      
      <bean name="userBoBean" class="com.affiliate.bo.UserBo">
        <property name="firstName">
          <value>Matthew</value>
        </property>
        <property name="lastName">
          <value>Penrice</value>
        </property>
      </bean>
      
    </beans>
    the xwork-related applicationContext-xwork.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
        "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
      
      <bean name="anAction" class="com.affiliate.actions.AnAction" singleton="false" depends-on="baseAction">
        <property name="attribute">
          <value>Fuxkkushdgiuer</value>
        </property>
      </bean>
      
    </beans>

    xwork.xml:
    Code:
    <!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN"
    "http&#58;//www.opensymphony.com/xwork/xwork-1.0.dtd">
    
    <xwork>
            <!-- Include webwork defaults &#40;from WebWork-2.1 JAR&#41;. -->
            <include file="webwork-default.xml"/>
    
            <!-- Configuration for the default package. -->
            <package name="default" extends="webwork-default">
    
            <action name="testAction" class="anAction">
              <result name="success" type="velocity">success.vm</result>
            </action>
     
            </package>
    </xwork>

    and finally web.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" 
    	xmlns="http&#58;//java.sun.com/xml/ns/j2ee" 
    	xmlns&#58;xsi="http&#58;//www.w3.org/2001/XMLSchema-instance" 
    	xsi&#58;schemaLocation="http&#58;//java.sun.com/xml/ns/j2ee 
    	http&#58;//java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      
      <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext*.xml</param-value>
      </context-param>
      
      <!-- the spring context loader listener -->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <!-- the spring-xwork integration module -->
      <listener>
        <listener-class>com.opensymphony.xwork.spring.SpringObjectFactoryListener</listener-class>
      </listener>
      
      <servlet>
        <servlet-name>webwork</servlet-name>
        <servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      
      <servlet>
        <servlet-name>webworkVelocity</servlet-name>
        <servlet-class>com.opensymphony.webwork.views.velocity.WebWorkVelocityServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
      </servlet>
      
      <servlet-mapping>
        <servlet-name>webwork</servlet-name>
        <url-pattern>*.action</url-pattern>
      </servlet-mapping>
        
      <servlet-mapping>
        <servlet-name>webworkVelocity</servlet-name>
        <url-pattern>*.vm</url-pattern>
      </servlet-mapping>
      
      <welcome-file-list>
        <welcome-file>/index.html</welcome-file>
      </welcome-file-list>
    
    </web-app>

    As mentioned, the it breaks down with a NullPointerException:
    Code:
    java.lang.NullPointerException
    	at com.affiliate.actions.AnAction.execute&#40;AnAction.java&#58;15&#41;
    	at com.opensymphony.xwork.DefaultActionInvocation.invokeAction&#40;DefaultActionInvocation.java&#58;283&#41;
    	at com.opensymphony.xwork.DefaultActionInvocation.invoke&#40;DefaultActionInvocation.java&#58;166&#41;
    	at com.opensymphony.xwork.DefaultActionProxy.execute&#40;DefaultActionProxy.java&#58;116&#41;
    	at com.opensymphony.webwork.dispatcher.ServletDispatcher.serviceAction&#40;ServletDispatcher.java&#58;272&#41;
    	at com.opensymphony.webwork.dispatcher.ServletDispatcher.service&#40;ServletDispatcher.java&#58;237&#41;
    	at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;809&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;ApplicationFilterChain.java&#58;200&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationFilterChain.java&#58;146&#41;
    	at org.apache.catalina.core.StandardWrapperValve.invoke&#40;StandardWrapperValve.java&#58;209&#41;
    	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext&#40;StandardPipeline.java&#58;596&#41;
    	at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;433&#41;
    	at org.apache.catalina.core.ContainerBase.invoke&#40;ContainerBase.java&#58;948&#41;
    	at org.apache.catalina.core.StandardContextValve.invoke&#40;StandardContextValve.java&#58;144&#41;
    	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext&#40;StandardPipeline.java&#58;596&#41;
    	at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;433&#41;
    	at org.apache.catalina.core.ContainerBase.invoke&#40;ContainerBase.java&#58;948&#41;
    	at org.apache.catalina.core.StandardContext.invoke&#40;StandardContext.java&#58;2358&#41;
    	at org.apache.catalina.core.StandardHostValve.invoke&#40;StandardHostValve.java&#58;133&#41;
    	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext&#40;StandardPipeline.java&#58;596&#41;
    	at org.apache.catalina.valves.ErrorDispatcherValve.invoke&#40;ErrorDispatcherValve.java&#58;118&#41;
    	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext&#40;StandardPipeline.java&#58;594&#41;
    	at org.apache.catalina.valves.ErrorReportValve.invoke&#40;ErrorReportValve.java&#58;116&#41;
    	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext&#40;StandardPipeline.java&#58;594&#41;
    	at org.apache.catalina.valves.AccessLogValve.invoke&#40;AccessLogValve.java&#58;534&#41;
    	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext&#40;StandardPipeline.java&#58;594&#41;
    	at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;433&#41;
    	at org.apache.catalina.core.ContainerBase.invoke&#40;ContainerBase.java&#58;948&#41;
    	at org.apache.catalina.core.StandardEngineValve.invoke&#40;StandardEngineValve.java&#58;127&#41;
    	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext&#40;StandardPipeline.java&#58;596&#41;
    	at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;433&#41;
    	at org.apache.catalina.core.ContainerBase.invoke&#40;ContainerBase.java&#58;948&#41;
    	at org.apache.coyote.tomcat4.CoyoteAdapter.service&#40;CoyoteAdapter.java&#58;152&#41;
    	at org.apache.coyote.http11.Http11Processor.process&#40;Http11Processor.java&#58;799&#41;
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection&#40;Http11Protocol.java&#58;705&#41;
    	at org.apache.tomcat.util.net.TcpWorkerThread.runIt&#40;PoolTcpEndpoint.java&#58;577&#41;
    	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run&#40;ThreadPool.java&#58;683&#41;
    	at java.lang.Thread.run&#40;Thread.java&#58;534&#41;

    Any help you could give would help me so much - I've read just about every Spring article and book but can't get over this **basic** hurdle.

  • #2
    Why are you declaring baseAction in your spring application context? Do you ever use baseAction explicitly? Basically you need to set the userBO and testAttribute on anAction:

    Code:
      <bean name="anAction" class="com.affiliate.actions.AnAction" singleton="false" depends-on="baseAction">
        <property name="attribute">
          <value>Fuxkkushdgiuer</value>
        </property>
        <property name="userBo"><ref bean="userBoBean" /></property>
        <property name="testAttribute"><value>Fuckerty</value></property>
      </bean>
    or define BaseAction as abstract and set anAction to use it as a parent. Reading section 3.5 of http://www.springframework.org/docs/...nce/beans.html will give you more info

    Comment

    Working...
    X