【发布时间】:2013-07-24 08:47:15
【问题描述】:
我有一些在本地运行良好的代码,但是当我尝试在远程服务器上运行它时,它会引发空指针异常。它在尝试从 Velocity 获取模板时这样做。它第一次失败,之后每次都失败。
有问题的代码是这样的:
URL location = Thread.currentThread().getContextClassLoader().getResource("velocity.properties");
String fullPath = location.getPath();
log.debug("Path: " + fullPath);
Velocity.init(fullPath);
Template tmplt = Velocity.getTemplate( "template.html" ); //This line throws the error
日志显示路径正确,我可以验证文件是否存在。
用于初始化velocity的属性文件包含:
resource.loader = file
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path=/var/lib/tomcat6/webapps/geoip/WEB-INF/templates/template.html
file.resource.loader.cache = true
input.encoding=UTF-8
output.encoding=UTF-8
错误的堆栈跟踪如下所示:
SEVERE: Servlet.service() for servlet Jersey REST Service threw exception
java.lang.NullPointerException
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1533)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1514)
at org.apache.velocity.runtime.RuntimeSingleton.getTemplate(RuntimeSingleton.java:299)
at org.apache.velocity.app.Velocity.getTemplate(Velocity.java:358)
at ca.company.ipservice.models.MyClass.toHTML(MyClass.java:48)
我在 Google 上四处搜索并搜索了 StackOverflow,但找不到任何答案。有什么想法吗?
【问题讨论】:
-
顺便说一句,我在多线程环境中使用 Velocity 并多次调用 init() 时遇到过类似的错误消息,但它在数百次执行中只出现一次,但似乎很少见竞争条件虽然 RuntimeInstance.init() 是同步的,所以不确定它是如何发生的......
标签: java apache tomcat jersey velocity