You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Sven Meier <sv...@meiers.net> on 2002/03/11 21:13:38 UTC
Exception with NULL value and tool
Hi,
Velocity is delivering an instance of class Object to a tool when it should
be a null value I've set up the following simple testcase:
Template:
********
<html>
<body>
#foreach ($data in $datas)
$tool.format($data.value)
#end
</body>
</html>
Source:
********
import java.io.*;
import java.util.*;
import java.text.*;
import org.apache.velocity.*;
import org.apache.velocity.context.*;
import org.apache.velocity.app.*;
import org.apache.velocity.app.event.*;
public class Test {
public static void main(String[] args) throws Exception {
Velocity.init();
VelocityContext context = new VelocityContext();
context.put("tool", new NullFormat());
List datas = new ArrayList();
datas.add(new Data(null));
datas.add(new Data(new Date()));
datas.add(new Data(new Date()));
datas.add(new Data(new Date()));
context.put("datas", datas);
FileWriter writer = new FileWriter("test.txt");
Velocity.getTemplate("test.template.txt").merge(context, writer);
writer.close();
}
public static class Data {
private Date value;
public Data(Date value) {
this.value = value;
}
public Date getValue() {
return value;
}
}
public static class NullFormat extends Format {
private Format format = new SimpleDateFormat();
public StringBuffer format(Object obj, StringBuffer toAppendTo,
FieldPosition pos) {
System.out.println("format " + obj);
if (obj == null) {
toAppendTo.append("<null>");
} else {
format.format(obj, toAppendTo, pos);
}
return toAppendTo;
}
public Object parseObject (String source, ParsePosition status) {
return null;
}
}
}
I get the following output:
************************
format java.lang.Object@54d4b226
org.apache.velocity.exception.MethodInvocationException: Invocation of
method 'format' in class Test$NullFormat threw exception class
java.lang.IllegalArgumentException : Cannot format given Object as a Date
at
org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:308
)
at org.apache.velocity.runtime.parser.node.ASTReference.execute(Compiled
Code)
at
org.apache.velocity.runtime.parser.node.ASTReference.render(ASTReference.jav
a:238)
at
org.apache.velocity.runtime.parser.node.ASTBlock.render(ASTBlock.java:94)
at org.apache.velocity.runtime.directive.Foreach.render(Foreach.java:344)
at
org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.jav
a:153)
at
org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:27
1)
at org.apache.velocity.Template.merge(Template.java:296)
at Test.main(Test.java:33)
Here comes the strange thing: If you change the following lines ...
List datas = new ArrayList();
datas.add(new Data(null));
datas.add(new Data(new Date()));
datas.add(new Data(new Date()));
datas.add(new Data(new Date()));
context.put("datas", datas);
... to ...
List datas = new ArrayList();
datas.add(new Data(new Date()));
datas.add(new Data(null));
datas.add(new Data(new Date()));
datas.add(new Data(new Date()));
context.put("datas", datas);
... everything works as expected.
Any comments?
Sven
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Exception with NULL value and tool
Posted by Sven Meier <sv...@meiers.net>.
It's even simpler - try this one:
Template:
*********
<html>
<body>
#foreach ($date in $datas)
without tool: $date --- with tool: $tool.it($date)
#end
</body>
</html>
Source:
*******
import java.io.*;
import java.util.*;
import java.text.*;
import org.apache.velocity.*;
import org.apache.velocity.context.*;
import org.apache.velocity.app.*;
import org.apache.velocity.app.event.*;
public class Test {
public static void main(String[] args) throws Exception {
Velocity.init();
VelocityContext context = new VelocityContext();
context.put("tool", new Tool());
List datas = new ArrayList();
datas.add(null);
datas.add(new Date());
datas.add(new Date());
datas.add(new Date());
context.put("datas", datas);
FileWriter writer = new FileWriter("test.txt");
Velocity.getTemplate("test.template.txt").merge(context, writer);
writer.close();
}
public static class Tool {
public String it(Object obj) {
return obj == null ? "<null>" : obj.toString();
}
}
}
Result:
*******
<html>
<body>
without tool: $date --- with tool: java.lang.Object@683912
without tool: Tue Mar 12 10:39:03 GMT+01:00 2002 --- with tool: Tue
Mar 12 10:39:03 GMT+01:00 2002
without tool: Tue Mar 12 10:39:03 GMT+01:00 2002 --- with tool: Tue
Mar 12 10:39:03 GMT+01:00 2002
without tool: Tue Mar 12 10:39:03 GMT+01:00 2002 --- with tool: Tue
Mar 12 10:39:03 GMT+01:00 2002
</body>
</html>
Where does the object with toString()->"java.lang.Object@683912" come from
???
Can anybody give me a clue?
Sven Meier
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>