tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <>
Subject Re: Strange NullPointerException Error in t4.0b7
Date Tue, 04 Sep 2001 15:49:37 GMT

On Tue, 4 Sep 2001, Paul Kofon wrote:

> Date: Tue, 04 Sep 2001 12:03:45
> From: Paul Kofon <>
> Reply-To:
> To:
> Subject: Strange NullPointerException Error in t4.0b7
> Hi All,
> I recently downloaded and installed Tomcat 4.0 b7. I found it to be quite
> responsive and remarkably faster than it's predecessors. However, my joy was
> short-lived when I found out an application which I had written and
> extensively tested under Tomcat 3.2.2. was not working in the T4.0 b7
> environment - I kept getting a NullPointerException error. I snooped around
> a bit and found out that the following piece of code was the culprit:
> File directory = new File("../webapps/humantrack/forms");
> File files[] = directory.listFiles();
> Now, here's the strange thing: "directory" isn't null but the array,
> files[], is! This code works without change in T3.2.2 as files[] doesn't
> return a null value. What's wrong?

Your pathname ("../webapps/humantrack/forms") is relative, so it will be
resolved against the current working directory of the servlet container --
something that you should *not* be making any assumptions about.

You can solve your immediate problem with something like this ...

  String contextRoot =
  if (contextRoot != null) {
      File directory = new File(contextRoot + "/../humantrack/forms");
      File files[] = directory.listFiles();
  } else {
     ... error message ...

but even this will not work *unless* all your web applications are stored
underneath the "webapps" directory.  In general, you can make *no*
assumptions about this.

A more rigorous approach will often work (assuming that "/humantrack" is
the context path for a different web app):

  ServletContext sc = getServletContext().getContext("/humantrack/forms");
  if (sc != null) {
      File directory = new File(sc.getRealPath("/forms"));
      File files[] = directory.listFiles();
  } else {
      ... error message ...

but even this is not guaranteed, because servlet containers are allowed to
return null from ServletContext.getContext().

And *all* of the solutions above assume that you are running your web
applications from open directory structures, rather than from WAR files.
This is a common feature of containers, but is not guaranteed to be
available universally.  So, a new feature of the Servlet 2.3 API lets you
ask for URLs of resources within a web application in a portable way
(again, assuming that "/humantrack" is the context path for your other

  ServletContext sc = getServletContext().getContext("/humantrack/forms");
  Set paths = sc.getResourcePaths("/forms/");

and you can iterate through the paths.  Each of them will be a String that
you can use in calls like:

  InputStream is = sc.getResourceAsStream(path);

> Regards,
> Paul

Craig McClanahan

View raw message