You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-user@jakarta.apache.org by Mamadou Cisse <ma...@alatto.com> on 2004/09/29 16:54:43 UTC

java.lang.NullPointerException and RequestDispatcherWrapper.forward_aroundBody0

After spending almost long fruitless hours searching the net for clues, 
I couldn't find any better place to ask than this list.
I'm using cactus 1.6 to unit/integration test J2EE components, servlets 
in this particular case. My environment is Jetty/Cactus/Eclipse 3.0.
My test case extends the Cactus ServletTestCase class.
In the setUp method i create an instance of my servlet to be tested.
........
myServlet = new MyServlet();
........
In the beginMyTest(WebRequest request) method I set the URL as:
.......
request.setURL(
                    "myserver:port",
                    "/contextpath",
                    "/some/path/someCommand.pp", null, null);
.......
Now in the testMyTest() method I call myServlet's doPost method. like:
.......
            servlet.init(config);
            myServlet.doPost(request, response);
.......

In doPost a command object is created from the parameter request. This 
command object does some processing and then returns a String as URL to 
the doPost method which in turn tries to forward the call. The 
pseudocode looks like:
{pseudocode ---
Command command = CommandFactory.getCommand(request), //1
String urlStr = command.execute(); //2
RequestDispatcher disp = request.getRequestDispatcher(urlStr); //3
disp.forward(request, response); //4
--- pseudocode}
On the line //4 there is a NullPointerException. The reason is that the 
originalDispatcher in the RequestDispatcher instance is null.

Any hint would be greatly appreciated.

--------------------------------------------------
java.lang.NullPointerException
    at 
org.apache.cactus.server.RequestDispatcherWrapper.forward_aroundBody0(RequestDispatcherWrapper.java:71)
    at 
org.apache.cactus.server.RequestDispatcherWrapper.forward_aroundBody1$advice(RequestDispatcherWrapper.java:117)
    at 
org.apache.cactus.server.RequestDispatcherWrapper.forward(RequestDispatcherWrapper.java)
    at com.mycompany.mypackage.MyServlet.doPost(MyServlet.java:110)
    at 
com.mycompany.mypackage.test.MyIntegrationTestCase.testMyTest(MyIntegrationTestCase.java:58)
---------------------------------------------------

-- 
Regards,

Mamadou



Re: java.lang.NullPointerException and RequestDispatcherWrapper.forward_aroundBody0

Posted by Kazuhito SUGURI <su...@lab.ntt.co.jp>.
Hi,

In article <41...@alatto.com>,
Thu, 30 Sep 2004 09:50:43 +0100,
Mamadou Cisse <ma...@alatto.com> wrote: 
mamadou> I went through the code in the debugger. The thing is that the returned 
mamadou> instance of the RequestDispatcher is NON-NULL, but this instance has an 
mamadou> instance variable called originalDispatcher which is NULL and I believe 
mamadou> this to be the cause of the problem.
mamadou> Before instanciating a test suite, I set the cactus.contextURL to 
mamadou> http://localhost:aport/mycontext using a call to System.setProperty:
mamadou>         System.setProperty("cactus.contextURL", 
mamadou> "http://localhost:aport/mycontext");
mamadou> So the question is why originalDispatcher in the returned 
mamadou> RequestDispatcher is set to null when a Cactus instanciated 
mamadou> HttpServletRequestWrapper  is passed to a servlet's doPost method, which 
mamadou> in turn calls getRequestDispatcher on the HttpRequestDispatcherWrapper 
mamadou> instance?

Thank you. I believe I understand your problem better.

Following is the getRequestDispatcher method of the AbstractHttpServletRequestWrapper:
    public RequestDispatcher getRequestDispatcher(String thePath)
    {
        if (thePath == null)
        {
            return null;
        }
 
        RequestDispatcher dispatcher = null;
        String fullPath;

        if (thePath.startsWith("/"))
        {
            fullPath = thePath;
        }
        else
        {
            String pI = getPathInfo();
            if (pI == null)
            {
                fullPath = catPath(getServletPath(), thePath);
            }
            else
            {
                fullPath = catPath(getServletPath() + pI, thePath);
            }

            if (fullPath == null)
            {
                return null;
            }
        }

        LOGGER.debug("Computed full path : [" + fullPath + "]");

        dispatcher = new RequestDispatcherWrapper(
            this.request.getRequestDispatcher(fullPath));

        return dispatcher;
    }


Your case can be occured if
    this.request.getRequestDispatcher(fullPath)
returns null because null-check for the originalDispatcher is not taken
in the constructor of the RequestDispatcherWrapper.
Here, this.request should be a HttpServletRequest instance
given from your container.
For fullPath value, please follow the logic of the code above.

Is the fullPath value computed from your command.execute() result,
which is given as the argument thePath, valid for your container?
You can also logging the real fullPath value.

Regards,
----
Kazuhito SUGURI
mailto:suguri.kazuhito@lab.ntt.co.jp

Re: java.lang.NullPointerException and RequestDispatcherWrapper.forward_aroundBody0

Posted by Mamadou Cisse <ma...@alatto.com>.
Hi Kazuhito and All the others.
I went through the code in the debugger. The thing is that the returned 
instance of the RequestDispatcher is NON-NULL, but this instance has an 
instance variable called originalDispatcher which is NULL and I believe 
this to be the cause of the problem.
Before instanciating a test suite, I set the cactus.contextURL to 
http://localhost:aport/mycontext using a call to System.setProperty:
        System.setProperty("cactus.contextURL", 
"http://localhost:aport/mycontext");
So the question is why originalDispatcher in the returned 
RequestDispatcher is set to null when a Cactus instanciated 
HttpServletRequestWrapper  is passed to a servlet's doPost method, which 
in turn calls getRequestDispatcher on the HttpRequestDispatcherWrapper 
instance?

Regards,
Mamadou.

>Hi,
>
>In article <41...@alatto.com>,
>Wed, 29 Sep 2004 15:54:43 +0100,
>Mamadou Cisse <ma...@alatto.com> wrote: 
>mamadou> In doPost a command object is created from the parameter request. This 
>mamadou> command object does some processing and then returns a String as URL to 
>mamadou> the doPost method which in turn tries to forward the call. The 
>mamadou> pseudocode looks like:
>mamadou> {pseudocode ---
>mamadou> Command command = CommandFactory.getCommand(request), //1
>mamadou> String urlStr = command.execute(); //2
>mamadou> RequestDispatcher disp = request.getRequestDispatcher(urlStr); //3
>mamadou> disp.forward(request, response); //4
>mamadou> --- pseudocode}
>mamadou> On the line //4 there is a NullPointerException. The reason is that the 
>mamadou> originalDispatcher in the RequestDispatcher instance is null.
>
>ServletRequest#getRequestDispatcher(String) returns null
>if the servlet container cannot return a RequestDispatcher.
>So, I think, the point is that the urlStr value returned from command.execute()
>is valid or not.
>
>You might separate the test into some parts like:
>    public void testCommand {
>        Command command = CommandFactory.getCommand(request);
>        assertEquals("expectedUrl", command.execute());
>    }
>    public void testDispatch {
>        assertNotNull(request.getRequestDispatcher("expectedUrl"));
>    }
>
>I guess testCommand() fails and/or your expectedUrl is not valid
>for your container.
>
>Hope this helps,
>----
>Kazuhito SUGURI
>mailto:suguri.kazuhito@lab.ntt.co.jp
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: cactus-user-help@jakarta.apache.org
>
>
>  
>


-- 
Regards,

Mamadou Cisse
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alatto Technologies
www.alatto.com
t: + 353 1 209 0785
f: + 353 1 209 0707
email: mamadou.cisse@alatto.com



Re: java.lang.NullPointerException and RequestDispatcherWrapper.forward_aroundBody0

Posted by Kazuhito SUGURI <su...@lab.ntt.co.jp>.
Hi,

In article <41...@alatto.com>,
Wed, 29 Sep 2004 15:54:43 +0100,
Mamadou Cisse <ma...@alatto.com> wrote: 
mamadou> In doPost a command object is created from the parameter request. This 
mamadou> command object does some processing and then returns a String as URL to 
mamadou> the doPost method which in turn tries to forward the call. The 
mamadou> pseudocode looks like:
mamadou> {pseudocode ---
mamadou> Command command = CommandFactory.getCommand(request), //1
mamadou> String urlStr = command.execute(); //2
mamadou> RequestDispatcher disp = request.getRequestDispatcher(urlStr); //3
mamadou> disp.forward(request, response); //4
mamadou> --- pseudocode}
mamadou> On the line //4 there is a NullPointerException. The reason is that the 
mamadou> originalDispatcher in the RequestDispatcher instance is null.

ServletRequest#getRequestDispatcher(String) returns null
if the servlet container cannot return a RequestDispatcher.
So, I think, the point is that the urlStr value returned from command.execute()
is valid or not.

You might separate the test into some parts like:
    public void testCommand {
        Command command = CommandFactory.getCommand(request);
        assertEquals("expectedUrl", command.execute());
    }
    public void testDispatch {
        assertNotNull(request.getRequestDispatcher("expectedUrl"));
    }

I guess testCommand() fails and/or your expectedUrl is not valid
for your container.

Hope this helps,
----
Kazuhito SUGURI
mailto:suguri.kazuhito@lab.ntt.co.jp