Announcement Announcement Module
Collapse
No announcement yet.
ActionSupport NullPointerException on context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ActionSupport NullPointerException on context

    I've been trying to obtain my ApplicationContext using ActionSupport and am getting a NullPointerException. I'm sure it's something trivial that I'm forgetting, I've just been staring at it too long so was looking for a little help.

    Here's my listener in web.xml:

    Code:
    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    
    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>
    My (now gutted) BaseAction to retrieve the context:

    Code:
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    import org.springframework.web.struts.ActionSupport;
    
    public class BaseAction extends ActionSupport &#123;
    
        protected static Log log = LogFactory.getLog&#40;BaseAction.class&#41;;
        protected String ctxString = getWebApplicationContext&#40;&#41;.toString&#40;&#41;;
        
    &#125;
    Any reference to getWebApplicationContext() in here won't work. I just get a NullPointerException from that line.

    Does anyone have any idea what the problem is? I'm using Tomcat 5.0.

    Thanks in advance,
    Mike

  • #2
    solution

    The problem is that the application context is not initialised until your object is intialised.
    i.e. - you can't refer to it in your field declarations or your constructor method.
    I just hit the same problem you see.

    Comment

    Working...
    X