You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Mead Lai <la...@gmail.com> on 2008/04/29 10:08:37 UTC

hi, I got some problem in velocity 1.5

Null reference [template 'control.vm', line 5, column 10] : $user.name
cannot be resolved.

the following is java code:
{
Template template = Velocity.getTemplate("control.vm", "GBK");
		VelocityContext ctx = new VelocityContext();

		List l= new ArrayList();
		l.add("pp");
		l.add("gg");
		ctx.put("list",l );
		class User{
			public String name="mead";
			public String pswd="admin";
			public String getName() {
				return name;
			}
			public void setName(String name) {
				this.name = name;
			}
			public String getPswd() {
				return pswd;
			}
			public void setPswd(String pswd) {
				this.pswd = pswd;
			}
		}
		User user = new User();
		ctx.put("user",user);
		this.print(user.name);
		FileOutputStream fos = new
FileOutputStream(Config.ACTION_Path+"TestControl.java");
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos));
		template.merge(ctx, writer);
		writer.flush();
		writer.close();
		fos.close();
}

########################################
here is the Velocity Template
## It is ok,and the list can iterator ok
${title}
## It is null reference
$user.name
${user.name}
$user.getName()
${user.getName()}
######################

I really dont know the issue. I think the code and template syntax is ok.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: hi, I got some problem in velocity 1.5

Posted by Adrian Tarau <ad...@gmail.com>.
That's true, your classes/methods have to be public. I'm not sure if is 
possible right now, but it will be useful to have one warning per 
class/method in such cases. The logging level should be adjustable, 
sometime it is useful to log them as errors so you will know when they 
fail during test unit.

For example : you can have a public class and somebody decides it can be 
private without knowing it is referenced in a template(the code compiles 
just fine). The application is deployed and it won't work as 
expected.You will look for hours until you will realize the real cause.


apache@recks.org wrote:
> Hi,
>
> try using "public class User ...". I suspect velocity fails to 
> introspect non-public classes.
>
> Regards,
> Christoph
>
> Mead Lai wrote:
>> Null reference [template 'control.vm', line 5, column 10] : $user.name
>> cannot be resolved.
>>
>> the following is java code:
>> {
>> Template template = Velocity.getTemplate("control.vm", "GBK");
>>         VelocityContext ctx = new VelocityContext();
>>
>>         List l= new ArrayList();
>>         l.add("pp");
>>         l.add("gg");
>>         ctx.put("list",l );
>>         class User{
>>             public String name="mead";
>>             public String pswd="admin";
>>             public String getName() {
>>                 return name;
>>             }
>>             public void setName(String name) {
>>                 this.name = name;
>>             }
>>             public String getPswd() {
>>                 return pswd;
>>             }
>>             public void setPswd(String pswd) {
>>                 this.pswd = pswd;
>>             }
>>         }
>>         User user = new User();
>>         ctx.put("user",user);
>>         this.print(user.name);
>>         FileOutputStream fos = new
>> FileOutputStream(Config.ACTION_Path+"TestControl.java");
>>         BufferedWriter writer = new BufferedWriter(new 
>> OutputStreamWriter(fos));
>>         template.merge(ctx, writer);
>>         writer.flush();
>>         writer.close();
>>         fos.close();
>> }
>>
>> ########################################
>> here is the Velocity Template
>> ## It is ok,and the list can iterator ok
>> ${title}
>> ## It is null reference
>> $user.name
>> ${user.name}
>> $user.getName()
>> ${user.getName()}
>> ######################
>>
>> I really dont know the issue. I think the code and template syntax is 
>> ok.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: hi, I got some problem in velocity 1.5

Posted by ap...@recks.org.
Hi,

try using "public class User ...". I suspect velocity fails to introspect non-public classes.

Regards,
Christoph

Mead Lai wrote:
> Null reference [template 'control.vm', line 5, column 10] : $user.name
> cannot be resolved.
> 
> the following is java code:
> {
> Template template = Velocity.getTemplate("control.vm", "GBK");
> 		VelocityContext ctx = new VelocityContext();
> 
> 		List l= new ArrayList();
> 		l.add("pp");
> 		l.add("gg");
> 		ctx.put("list",l );
> 		class User{
> 			public String name="mead";
> 			public String pswd="admin";
> 			public String getName() {
> 				return name;
> 			}
> 			public void setName(String name) {
> 				this.name = name;
> 			}
> 			public String getPswd() {
> 				return pswd;
> 			}
> 			public void setPswd(String pswd) {
> 				this.pswd = pswd;
> 			}
> 		}
> 		User user = new User();
> 		ctx.put("user",user);
> 		this.print(user.name);
> 		FileOutputStream fos = new
> FileOutputStream(Config.ACTION_Path+"TestControl.java");
> 		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos));
> 		template.merge(ctx, writer);
> 		writer.flush();
> 		writer.close();
> 		fos.close();
> }
> 
> ########################################
> here is the Velocity Template
> ## It is ok,and the list can iterator ok
> ${title}
> ## It is null reference
> $user.name
> ${user.name}
> $user.getName()
> ${user.getName()}
> ######################
> 
> I really dont know the issue. I think the code and template syntax is ok.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org