null pointer exception (Action servlet is not initialized)

2013-09-05
2013-09-05
  • Mohammed Nabih

    Mohammed Nabih - 2013-09-05

    hello
    I'm using Junit 4.10 , strutstestcase 2.1.2 , and struts 1.1
    i tested a demo project and it worked effectively 100%
    I did the same configurations with the real project i got the null pointer exception
    it happens when i call the actionPerform() or by calling teardown() because both of them use the actionServlet object and its null

    and I did set the correct context directory and request path and sure they are correct.
    I think the problem is with struts-config.xml and its under web-inf , when i set it's path it gives the same exception.

    when i set invalid web.xml path using setContextDirectory() it says invalid path , when I don't use it it says nothing
    and when i give a valid path it gives a parsing error , but i think its not a problem it happened with the demo project and no problem happened and test was successful.

    I get this log
    .11:17:33,137 ERROR [main] org.apache.struts.action.ActionServlet -
    java.net.MalformedURLException
    at java.net.URL.<init>(URL.java:601)
    at java.net.URL.<init>(URL.java:464)
    at java.net.URL.<init>(URL.java:413)
    at
    .
    .
    .
    at -------------------------------------------.main(LoginActionTest.java:62)
    11:17:33,170 ERROR [main] org.apache.struts.action.ActionServlet -
    java.net.MalformedURLException
    at java.net.URL.<init>(URL.java:601)
    at java.net.URL.<init>(URL.java:464)
    at java.net.URL.<init>(URL.java:413)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:650)
    at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java.
    .
    .
    .

    11:17:33,175 ERROR [main] servletunit.struts.MockStrutsTestCase - Error initializing action servlet
    javax.servlet.ServletException
    at org.apache.struts.action.ActionServlet.initServlet(ActionServlet.java:1437)
    at org.apache.struts.action.ActionServlet.init(ActionServlet.java:466)
    .
    .
    .
    .
    E
    Time: 0.237
    There was 1 error:
    1) testSuccessfulLogin(------------.LoginActionTest)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 -------------- directory. Context directory is ------. 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)
    at -------------------LoginActionTest.testSuccessfulLogin(LoginActionTest.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at -------------------------.LoginActionTest.main(LoginActionTest.java:62)


    Root Cause:

    java.lang.NullPointerException
    at servletunit.struts.MockStrutsTestCase.getActionServlet(MockStrutsTestCase.java:331)
    at servletunit.struts.MockStrutsTestCase.actionPerform(MockStrutsTestCase.java:394)
    at ----------------------.testSuccessfulLogin(LoginActionTest.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at ---------------------------.LoginActionTest.main(LoginActionTest.java:62)

    FAILURES!!!
    Tests run: 1, Failures: 0, Errors: 1

    Process exited with exit code 0.

    thats my nightmare :) I thank you very much in advance , thanks

     
    Last edit: Mohammed Nabih 2013-09-05
  • Mohammed Nabih

    Mohammed Nabih - 2013-09-05

    solved , the context path was set wrong ,
    it should be
    setContextDirectory(new File("-----\public_html"));
    not
    setContextDirectory(new File("------public_html\web.xml"));
    Thanks

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks