You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by David Esposito <es...@newnetco.com> on 2001/03/15 15:48:53 UTC

NullPointerException for undefined member of hashtable

I'm wondering if this is normal behavior ... I have the following situation
(a little bit pseudocoded)

public class MetaDataObject
{
	public MetaDataObject(String a)
	{fName = a;}

	public String fName;

	public String getFirstName()
	{ return fName;}
}

Hashtable metaData = new Hashtable()

metaData.put("Programmer",new MetaDataObject("Dave"));
metaData.put("Administrator",new MetaDataObject("Bob"));
metaData.put("Secretary",new MetaDataObject("Jane"));

ctx.put("metaData",metaData);

in my VTL i do

$!metaData.Programmer.FirstName

$!metaData.Administrator.FirstName

$!metaData.Secretary.FirstName

$!metaData.Billing.FirstName


But since Billing is not in the Hashtable, Velocity throws a NPE and dumps
the stack to the console ... It would be nice if it just caught this
exception and maybe just logged it to the log file or something?

Example:

 java.lang.NullPointerException
        at org.apache.velocity.runtime.parser.node.ASTIdentifier
.execute(ASTIdentifier.java:159)
        at org.apache.velocity.runtime.parser.node.ASTReference.
execute(ASTReference.java:159)
        at org.apache.velocity.runtime.parser.node.ASTReference.
render(ASTReference.java:174)
        at org.apache.velocity.runtime.parser.node.SimpleNode.re
nder(SimpleNode.java:255)
        at org.apache.velocity.Template.merge(Template.java:281)

        at org.apache.velocity.servlet.VelocityServlet.mergeTemp
late(VelocityServlet.java:313)
        at org.apache.velocity.servlet.VelocityServlet.doRequest
(VelocityServlet.java:258)
        at org.apache.velocity.servlet.VelocityServlet.doPost(Ve
locityServlet.java:213)
        at javax.servlet.http.HttpServlet.service(HttpServlet.ja
va:760)
        at javax.servlet.http.HttpServlet.service(HttpServlet.ja
va:853)
        at org.apache.tomcat.core.ServletWrapper.doService(Servl
etWrapper.java:404)
        at org.apache.tomcat.core.Handler.service(Handler.java:2
86)
        at org.apache.tomcat.core.ServletWrapper.service(Servlet
Wrapper.java:372)
        at org.apache.tomcat.core.ContextManager.internalService
(ContextManager.java:797)
        at org.apache.tomcat.core.ContextManager.service(Context
Manager.java:743)
        at org.apache.tomcat.service.http.HttpConnectionHandler.
processConnection(HttpConnectionHandler.java:210)
        at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolT
cpEndpoint.java:416)
        at org.apache.tomcat.util.ThreadPool$ControlRunnable.run
(ThreadPool.java:498)
        at java.lang.Thread.run(Unknown Source)
 ASTIdentifier.execute() : identifier = MaxLength : java.lang.Nu
llPointerException


RE: velocity punks out on call to method on undefined value

Posted by David Esposito <es...@newnetco.com>.

> From: gmj@mta8.srv.hcvlny.cv.net [mailto:gmj@mta8.srv.hcvlny.cv.net]On
> Behalf Of Geir Magnusson Jr.
>
> > David Esposito wrote:
> >
> > Hey ... me again ... i've been quiet for too long ... ;)
> >
>
> And posting in HTML :)

Yah, there's a long and convoluted reason that happened ... and of course
it's not my fault ... ;) ... i'll try harder next time .. :)

>
> > Here's the situation:
> >
> > my VTL:
> >
> > #if($item.member_company_name.length() > 0)
> >     Supplier: $item.member_company_name
> > #else
> >     &lt;Your company&gt;
> > #end
> >
> > If item.member_company_name is not defined in the context, velocity
> > disappears from the scene and doesn't finish rendering the template
> > ... no error is written to the log file ... everything seems to be
> > going swimmingly except that the HTML is truncated at the call to the
> > length() method ... ideally, I'd like to have the #if() evaluate to
> > false if the item isn't defined in the context ...
>
> Ja.  I that fixed that one two nights ago. :)
>
> get the latest and see if that fixes it.


Yep works like a charm ... and it writes out a nice line to the log file ...


>
> I need another day to scrounge the time to do a few more related things,
> make a test case, test, and then will have a new 'update release' with
> that and related fixes.
>
> Sorry.
>

No problem ... i don't think anyone could complain about 1 hour bug fix
turnaround time ... ;)


Re: velocity punks out on call to method on undefined value

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
> David Esposito wrote:
> 
> Hey ... me again ... i've been quiet for too long ... ;)
> 

And posting in HTML :)

> Here's the situation:
> 
> my VTL:
> 
> #if($item.member_company_name.length() > 0)
>     Supplier: $item.member_company_name
> #else
>     &lt;Your company&gt;
> #end
> 
> If item.member_company_name is not defined in the context, velocity
> disappears from the scene and doesn't finish rendering the template
> ... no error is written to the log file ... everything seems to be
> going swimmingly except that the HTML is truncated at the call to the
> length() method ... ideally, I'd like to have the #if() evaluate to
> false if the item isn't defined in the context ...

Ja.  I that fixed that one two nights ago. :)

get the latest and see if that fixes it.

I need another day to scrounge the time to do a few more related things,
make a test case, test, and then will have a new 'update release' with
that and related fixes.

Sorry.


-- 
Geir Magnusson Jr.                               geirm@optonline.net
Developing for the web?  See http://jakarta.apache.org/velocity/

velocity punks out on call to method on undefined value

Posted by David Esposito <es...@newnetco.com>.
Hey ... me again ... i've been quiet for too long ... ;)

Here's the situation:

my VTL:

#if($item.member_company_name.length() > 0)
    Supplier: $item.member_company_name
#else
    &lt;Your company&gt;
#end

If item.member_company_name is not defined in the context, velocity
disappears from the scene and doesn't finish rendering the template ... no
error is written to the log file ... everything seems to be going swimmingly
except that the HTML is truncated at the call to the length() method ...
ideally, I'd like to have the #if() evaluate to false if the item isn't
defined in the context ...

this is similar to the 'issue' that i reported a while back that Jason said
was fixed ... i've attached the copy below to refresh your memory .. ;)

thanks for all of your hard work ... you guys kick ass ... ;)

I'm using Velocity 1.0

-Dave


====================
Snippet from 'issue' report on 3/15/2001
----------------------------------------
I'm wondering if this is normal behavior ... I have the following situation

(a little bit pseudocoded)

public class MetaDataObject

{

public MetaDataObject(String a)

{fName = a;}

public String fName;

public String getFirstName()

{ return fName;}

}

Hashtable metaData = new Hashtable()

metaData.put("Programmer",new MetaDataObject("Dave"));

metaData.put("Administrator",new MetaDataObject("Bob"));

metaData.put("Secretary",new MetaDataObject("Jane"));

ctx.put("metaData",metaData);

in my VTL i do

$!metaData.Programmer.FirstName

$!metaData.Administrator.FirstName

$!metaData.Secretary.FirstName

$!metaData.Billing.FirstName



But since Billing is not in the Hashtable, Velocity throws a NPE and dumps

the stack to the console ... It would be nice if it just caught this

exception and maybe just logged it to the log file or something?


Re: NullPointerException for undefined member of hashtable

Posted by Jason van Zyl <jv...@apache.org>.
David Esposito wrote:

> I'm wondering if this is normal behavior ... I have the following situation
> (a little bit pseudocoded)
>
> public class MetaDataObject
> {
>         public MetaDataObject(String a)
>         {fName = a;}
>
>         public String fName;
>
>         public String getFirstName()
>         { return fName;}
> }
>
> Hashtable metaData = new Hashtable()
>
> metaData.put("Programmer",new MetaDataObject("Dave"));
> metaData.put("Administrator",new MetaDataObject("Bob"));
> metaData.put("Secretary",new MetaDataObject("Jane"));
>
> ctx.put("metaData",metaData);
>
> in my VTL i do
>
> $!metaData.Programmer.FirstName
>
> $!metaData.Administrator.FirstName
>
> $!metaData.Secretary.FirstName
>
> $!metaData.Billing.FirstName
>
> But since Billing is not in the Hashtable, Velocity throws a NPE and dumps
> the stack to the console ... It would be nice if it just caught this
> exception and maybe just logged it to the log file or something?
>
>

Thanks for spotting that, fixed :-)


--
jvz.

Jason van Zyl
jvanzyl@periapt.com

http://jakarta.apache.org/velocity
http://jakarta.apache.org/turbine
http://tambora.zenplex.org