Re: question (related to now I know why I was initializing Map in constructor)...

Vic Cekvenich <[email protected]> Fri, 06 Jun 2003 19:48:53 -0400
Newsgroups gmane.comp.java.mvc.devel
Organization baseBeans Engineering
Message-ID <[email protected]>

Rick Reumann wrote:

>Remember a few days ago I posted about that I was doing :
>	_current = new Map();
>
>Now I know why I was doing that, (although I think I found a better
>plan)... anyway the reason I was having to initialize the Map is
>sometimes the insertNew() method of BaseBean wasn't called (which sets
>up a new _current Map into the list) because I was using a FormBean
>directly from a link. 
>
>For example I have a list of projects (ProjectBeans) and when I click on
>one of the projects I go to a ProjectDetailAction which queries and
>returns me more detailed project information. The link looks like:
>
><html:link forward="projectDetail" paramName="element"
>paramProperty="projectId" paramId="projectId"><c:out
>value="${element.projectId}"/></html:link>
>
>The problem is this projectDetail maps to an Action that has a
>ProjectSearchBean associated with it and since I'm not going through a
>"New" method, there is no _current Map that ever gets intialized so you
>end up with null pointer problems in the setProjectId(..) method since
>it calls the BaseBean setValue() method which tries to populate the non
>initialized _current Map. 
>
>So the question is, how do I get around this problem? I'm thinking maybe
>I should do a quick check in the BaseBean setValue() method ..
>
>if ( _current == null ) {
>	_current = new Map();
>}
>
This method I think gets called a lot, by each getter, so I would not 
add that pcode to it at a hot spot. (but in general checking for null is 
good since null is a bad exception to ever get)
My preference is that your project Detail action was called via a 
dispatch new event parm (you can pass mutiple parms, I use JSTL); and in 
the new event of action you prep your bean (MVC Style; you know I do not 
like to mix beans/actions, to me each form (page) has it's own 
formBean..... generally). I do have this kind of thing in a few places 
of bP.

Also... I might have know more Struts than you before; at this point we 
are more peers, so I am not sure if I can be the one to always 
recommend, it be more of an opinion where I am right 50%. But keep 
asking for comments.

>
>But maybe that's not such a good idea? (The other idea was how I
>initially was doing it, setting _current = new Map() in the bean
>constructor. 
>  
>
The guide line there is : do majority of beans need it? (not! so design 
for the rule, and not "rate" cases. If you handle rare cases, you get 
complexity)

>Any other ideas? suggestions? Thanks.
>
>
>  
>

-- 
Vic Cekvenich,
Struts Instructor,
1-800-917-JAVA

Advanced <a href ="baseBeans.com">Struts Training</a> and project recovery in North East.
Open Source <a href ="baseBeans.com">Content Management</a>  basicPortal sofware
Best practice<a href ="baseBeans.com">Struts Support</a> v.1.1 helper ScafflodingXPress