Menu

actionPerform() NullPointerException

2009-11-10
2013-09-05
  • Bernd Schatz

    Bernd Schatz - 2009-11-10

    I got a NullPointerException whenever i execute actionPerform()

    ****
    servletunit.struts.ExceptionDuringTestError: A NullPointerException was thrown. 
    This may indicate an error in your ActionForm, or it may indicate that the Struts ActionServlet was unable to find struts config file. 
    TestCase is running from /home/user/workspace/APP directory. 
    Context directory is /home/user/workspace/APP. 
    struts config file must be found under the context directory, the directory the test case is running from, or in the classpath.
    at servletunit.struts.MockStrutsTestCase.actionPerform(MockStrutsTestCase.java:407)
    *****
    The ContextDirectory and config files seems to be fine,
    if  i use wrong paths i got other errors.

    *****
    setContextDirectory(new File("/home/user/workspace/APP"));
    setConfigFile("WEB-INF/struts-config.xml");
    setServletConfigFile("WEB-INF/web.xml");
    *********

    I have found many of this error reports on google, but no solutions.
    any ideas ?

     
  • Patrick

    Patrick - 2009-11-10

    You shouldn't have to use setServletConfigFile("WEB-INF/web.xml");
    So try removing it.

    setContextDirectory should find the web.xml on it's own e.g. it takes the Dir specified by setContextDirectory and looks for web.xml at WEB-INF/web.xml
    So is /home/user/workspace/APP/WEB-INF/web.xml a valid location?

     
  • Bernd Schatz

    Bernd Schatz - 2009-11-11

    Hi,

    If i change it to  setServletConfigFile("WEB-INF/web2.xml");

    i got this error:

    junit.framework.AssertionFailedError: Invalid pathname: WEB-INF/web2.xml
    at servletunit.struts.MockStrutsTestCase.setServletConfigFile(MockStrutsTestCase.java:602)


    So i  think the path with "WEB-INF/web.xml" is valid.

    If i remove setServletConfigFile("WEB-INF/web.xml");

    I got the same error as described:

    servletunit.struts.ExceptionDuringTestError: A NullPointerException was thrown.  This may indicate an error in your ActionForm, or it may indicate that the Struts ActionServlet was unable to find struts config file.  TestCase is running from /home/bschatz/workspace/UMA directory.  Context directory is /home/bschatz/workspace/UMA.  struts config file must be found under the context directory, the directory the test case is running from, or in the classpath.
    at servletunit.struts.MockStrutsTestCase.actionPerform(MockStrutsTestCase.java:407)


    Root Cause:

    java.lang.NullPointerException
    at servletunit.struts.MockStrutsTestCase.getActionServlet(MockStrutsTestCase.java:331)
    at servletunit.struts.MockStrutsTestCase.actionPerform(MockStrutsTestCase.java:394)

     
  • Patrick

    Patrick - 2009-11-11

    Can you check to see if getActionServlet(); is returning null?

    e.g. In your test case before calling actionPerform(); call assertNotNull(getActionServlet());

    Is your Action class in your classpath when running the test?

    Perhaps if I could see your struts-config.xml and testCase (along with anything in the setUp method) I would have better insight.

     
  • Mohammed Nabih

    Mohammed Nabih - 2013-09-05

    I have this problem , getActionServlet() returns null , actionPerform() and tearDown() throw null pointer exception because of this null object

     

Log in to post a comment.