I’ve been using Velocity for quite some time and never had an issue with it. However, recently, i came across an issue.
I’m using velocity to generate HTML code and send the generated HTML as an email. This process works 50% of the time. When it fails im getting this error:
Caused by: java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:61)
at java.io.InputStreamReader.<init>(InputStreamReader.java:80)
at org.apache.commons.collections.ExtendedProperties.load(ExtendedProperties.java:542)
at org.apache.commons.collections.ExtendedProperties.load(ExtendedProperties.java:519)
at org.apache.velocity.runtime.RuntimeInstance.setDefaultProperties(RuntimeInstance.java:397)
at org.apache.velocity.runtime.RuntimeInstance.initializeProperties(RuntimeInstance.java:570)
at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:250)
at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:590)
at org.apache.velocity.app.VelocityEngine.init(VelocityEngine.java:136)
at testapp.webapp.utility.velocity.VelocitySettings.<init>(VelocitySettings.java:48)
... 26 more
My code is as follows:
VelocityEngine ve = null;
try
{
Properties props = new Properties();
props.put("resource.loader", "file");
props.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
props.put("file.resource.loader.path", Settings.getVelocityTemplatesPath());
props.put("file.resource.loader.cache", "true");
props.put("runtime.log.logsystem.class","org.apache.velocity.runtime.log.NullLogSystem");
ve = new VelocityEngine();
ve.init(props); //THE ERROR IS TAKING PLACE HERE
}
catch (Exception e)
{
throw new Exception ("Error while instantiating the velocity engine", e);
}
I still can’t explain why it’s happening randomly. I researched a bit about this but couldn’t find any clue yet. Anybody out there?
It seems I might have fixed the issue.
Unfortunately, in the problem statement above, I forgot to mention a very important detail. The code above is being executed multiple times. I have 10s or 100s of quartz jobs running, and each job will eventually execute the code above. This means, that each fired job will re-init VelocityEngine. Why this kind of behavior is causing the error above is something still beyond me. In order to overcome this issue, the code above resides in a normal JAVA class. I had to convert that class to a Singleton so that the initiation of VelcoityEngine is done always once. After these changes, the errors stopped streaming.