我有一些在本地工作得很好的代碼,但是當我嘗試在遠程服務器上運行它時,它會拋出一個空指針異常。它在嘗試從Velocity獲取模板時會這樣做。它第一次失敗,每次都失敗。Velocity在獲取模板時拋出NPE
有問題的代碼是該位:
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
記錄顯示的路徑是正確的,我可以驗證該文件是存在的。
用來初始化速度的屬性文件包含:
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)
我周圍的一派,搜索的StackOverflow,但我不能找到任何答案。有任何想法嗎?
嘗試下面的鏈接 http://stackoverflow.com/questions/2931516/loading-velocity-template-inside-a-jar-file – sunshine
順便說一句,我有一個類似的錯誤消息曾經在多線程環境中使用速度和調用的init()多次時,數百處決的只有一次出現了,不過,好像雖然RuntimeInstance.init()同步是一個罕見的競爭條件,所以不知道怎麼會發生... – centic