简体   繁体   中英

Java Properties class throwing NPE when loading XML file within servlet?

All,

I have decided to adopt the java.util.Properties class within my servlets to facilitate easier maintenance of configs.

I am using the poperties class function loadFromXML to fetch an XML file that contains denatured (XML compliant ) SQL queries that my servlet will then execute.

Using this code which works fine in normal CLI Java application:

// retrieve all queries from xml from classpath
    queries = new Properties();
    try
    {
        String path = getServletContext().getRealPath("/WEB-INF");
        System.out.println(path + "/queries.xml");
        queries.loadFromXML(MyServlet.class.getResourceAsStream(path + "/queries.xml"));
    }
    catch (IOException io)
    {
        io.printStackTrace();
    }

The output is a NULL Pointer Exception thrown by the loadFromXML method

java.lang.NullPointerException at java.util.Properties.loadFromXML(Properties.java:851)

The file exists as produced by the system out message in the web server logs.

CORE3282: stdout: /u02/SunONE61060/testserver/myservlet/WEB-INF/queries.xml

I tried moving the XML file to the base directory where the Servlet class exists and call it from there but still the same NPE.

Any ideas?

It isn't valid to get a real path and then treat that as part of a resource path. It isn't. Make up your mind. Just do

getResourceAsStream("/WEB-INF/classes/.../queries.xml")

where the ... is the package of MyServlet, and put the resource file there.

Your problem is here:

MyServlet.class.getResourceAsStream(path + "/queries.xml")

This will return null . .getResourceAsStream() can only load from the classpath (for instance, /com/foo/myclass/MyResource.xml ).

Since you have the absolute path of your resource, just use a standard FileInputStream , for instance.

Also: close your stream after you are done with it . Right now you don't: you have a resource leak. See the javadoc for Closeable .

You are attempting to load the resources from your classpath but are providing a real path. These two are not the same.

When you are using

MyServlet.class.getResourceAsStream("queries.xml")

Then the classloader will attempt to load the class from whereever your MyServlet.class file is, so if the package is my.pkg , it will attempt to load it from WEB-INF/classes/my/pkg/queries.xml .

You can also put the queries into the root of your class hierarchy ( WEB-INF/classes/queries.xml ) and load it like so:

MyServlet.class.getResourceAsStream("/queries.xml")

Then it will expect the file in WEB-INF/classes/queries.xml .

Alternatively, if you want to leave the file where it is, just use the servlet context to get the input stream:

getServletContext().getResourceAsStream("/WEB-INF/queries.xml")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM