velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sven Meier" <s...@meiers.net>
Subject Exception with NULL value and tool
Date Mon, 11 Mar 2002 20:13:38 GMT
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:   <mailto:velocity-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:velocity-user-help@jakarta.apache.org>


Mime
View raw message