Hi Matthias (and Nathan),
Thanks for your quick replies.
I have changed my classes to public, and that helps a bit. Actually the
example I gave was a test I wrote to mimic my actual application's class
hierarchy, and in my real application I was only having a problem with the
$bo.get("a") scenario. Now my test exactly matches my real scenario, in that:
$bo.A
$bo.getA()
$bo.get("a")
$bo2.get($a)
Now yields:
1
1
$bo.get("a")
1
So I'm still confused; why doesn't $bo.get("a") work?
Sincerely,
Daryl.
-----Original Message-----
From: Matthias Hendler [mailto:[EMAIL PROTECTED]
Sent: Friday, October 06, 2006 11:15 AM
To: Velocity Users List
Subject: Re: Problem getting value from Map.
Hello Daryl,
well, your class is package private and it must be of public type.
You added a package private class into the velocity context.
This class is not accessible for velocity.
Declare the class BO public and it should work.
What else might confuse you - it confused me :-) - is, that if a method
returns "null" in java, velocity logs an invalid reference message.
This might happen for $Map.put(value). To ommit this, use the silent notation
like $!Map.put(value). This notation ignores the return value from the put
method.
Regards
Matthias
-------- Original-Nachricht --------
Datum: Fri, 6 Oct 2006 10:40:17 -0400
Von: "Daryl Beattie" <[EMAIL PROTECTED]>
An: [email protected]
Betreff: Problem getting value from Map.
> Hi Velocity people,
>
>
>
> Okay, first off; I tried searching for answers to this, but
> the mailing list archive is not searchable (in any way that I could
> find), so if this question is a faq, please don't be upset.
>
> I am having trouble getting a value out of a Map. Every time
> I try, the reference gets printed out as it is. Even normal methods
> don't seem to be functioning consistently.
>
> I have developed a test case to illustrate my problem. The
> code may look long but it's pretty simple:
>
>
>
> class BO extends BOBase {
>
> // Empty.
>
> }
>
>
>
> class BOBase extends DataHolder {
>
> // Empty.
>
> }
>
>
>
> class DataHolder extends HashMap<Name, Value> {
>
> public Value getA() {
>
> return this.get(new Name("a")); //$NON-NLS-1$
>
> }
>
> @Override
>
> public Value get(Object key) {
>
> return super.get(new Name(key.toString()));
>
> }
>
> }
>
>
>
> class Name {
>
> String name;
>
> public Name(String name) {
>
> this.name = name;
>
> }
>
> @Override
>
> public String toString() {
>
> return name;
>
> }
>
> @Override
>
> public boolean equals(Object a) {
>
> return ((a != null) && (a instanceof Name) &&
> a.toString().equals(this.toString()));
>
> }
>
> @Override
>
> public int hashCode() {
>
> return name.hashCode();
>
> }
>
> }
>
>
>
> class Value {
>
> String value;
>
> public Value(String value) {
>
> this.value = value;
>
> }
>
> @Override
>
> public String toString() {
>
> return value;
>
> }
>
> }
>
>
>
> And when I run velocity I do this:
>
>
>
> VelocityContext context = new VelocityContext();
>
>
>
> BO test = new BO();
>
> test.put(new Name("a"), new Value("1"));
>
> context.put("bo", test);
>
>
>
> Map<Name, Value> test2 = new HashMap<Name, Value>();
>
> test2.put(new Name("a"), new Value("1"));
>
> context.put("bo2", test2);
>
> context.put("a", new Name("a"));
>
>
>
> context.put("test", new String("Boo!"));
>
>
>
> My template looks like this:
>
>
>
> $bo.A
>
> $bo.getA()
>
> $bo.get("a")
>
>
>
> $bo2.get($a)
>
>
>
> $test
>
>
>
> But my output looks like this:
>
>
>
> $bo.A
>
> $bo.getA()
>
> $bo.get("a")
>
>
>
> 1
>
>
>
> Boo!
>
>
>
> What is going on?
>
>
>
> Sincerely,
>
>
>
> Daryl.
>
--
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]