Reputation: 117
I had a working Java Web application deployed as a war package in one server and when I moved it to another (both servers using Tomcat 7), I get the following error:
HTTP Status 500 - Servlet.init() for servlet amie.demo.AMIEServlet threw exception
java.lang.NullPointerException java.io.File.(File.java:277) amie.demo.AMIEServlet.init(AMIEServlet.java:92)
The code that causes the problem looks like this:
@Override
public void init(ServletConfig config) throws ServletException {
String kbPath = config.getServletContext().getInitParameter("kb-path");
String kbAbsPath = config.getServletContext().getRealPath(kbPath);
String metadataPath = config.getServletContext().getInitParameter("kb-metadata");
String metadataAbsPath = config.getServletContext().getRealPath(metadataPath);
try {
kb.load(new File(kbAbsPath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The problem is that config.getServletContext().getRealPath is returning null. The property "kb-path" referred in the snippet is defined in the web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>AMIEDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>kb-path</param-name>
<param-value>yago2core.10kseedsSample.decoded.compressed.notypes.nolangcode.usascii.tsv</param-value>
</context-param>
<context-param>
<param-name>kb-metadata</param-name>
<param-value>yagoWikipediaInfo.relevance.sample.tsv</param-value>
</context-param>
</web-app>
The file referenced by the "kb-path" property is located at the root of the .war package. As I said, this worked in the previous server, so I suspect there is a configuration issue. Any hint will be highly appreciated.
Upvotes: 1
Views: 3162
Reputation: 4254
if directory is not exist then it will create new directory.
String uploadsDir = "/uploads/";
String realPathtoUploads = request.getSession().getServletContext().getRealPath(uploadsDir);
if (!new File(realPathtoUploads).exists()) {
new File(realPathtoUploads).mkdir();
}
Upvotes: 0
Reputation: 691635
The resources are in a war file, not in the file system. So the method returns null, as documented.
Use ServletContext.getResourceAsStream()
to load the resources. Not file IO, since those aren't files, but resources embedded into the war file.
Upvotes: 2