5 Replies Latest reply on Mar 20, 2009 11:15 AM by buckmin.erdem.agaoglu.gmail.com

    Map, new-Operator and In

    nimo22

      In Seam, you should not instantiate a Object via the new-Operator. But what about this:


      @In(required=false, create=true) @Out(required=false) private Map<String, String> myMap;



      You see, I want seam to create a new Instance if no is available. But this does not work.



      When I want to use my Map I get a NPE. For example, in the GUI:



      <h:inputText value="#{myMap['test']} .../>



      This returns:



      value="#{myMap['test']}": Target Unreachable, identifier 'myMap' resolved to null




      It works, only when I instantiate my Map with the new-Operator:


      @In(required=false, create=true) @Out(required=false) private Map<String, String> myMap = new HashMap<String, String>();



      But I have thougth, I should not use the new-Operator in SEAM. So what I make wrong?


        • 1. Re: Map, new-Operator and In
          oguzyalcin.oguzhanyalcin.gazi.edu.tr

          Could you please try the code below. Maybe seam tries to create an instance using ...myMap=new Map<String,String>; ...so in this situation it is impossible to create an instance.



          @In(required=false, create=true) @Out(required=false) private HashMap<String, String> myMap;


          • 2. Re: Map, new-Operator and In
            nimo22

            I have tried it with HashMap


            @In(required=false, create=true) @Out(required=false) private HashMap<String, String> myMap;




            but the same failure occurs:


            Caused by: javax.el.PropertyNotFoundException: /mySite.xhtml @111,79 value="#{myMap['test']}": Target Unreachable, identifier 'myMap' resolved to null



            I solved it via new-Operator even Seam does not allow to use it.

            • 3. Re: Map, new-Operator and In
              buckmin.erdem.agaoglu.gmail.com
              First of all, i am not much of a expert but @In(required=false, create=true) seems a little weird. By required=false you tell seam that if there is no var named myMap in any context, then it is not a problem. By create=true you tell seam if there is no var myMap, then it should try to create it.

              i don't know the exact order but i think seam tries to create because of create=true but since there is no component definition for "myMap" it cant and gives no problem because of required=false. hence you get el exception.

              i often use @Factory for non-javabean non-ejb context variable initialization. so if you have

              @Factory("myMap")
              public void initMyMap(){
                  myMap = new HashMap<String, String>();
                  // population of that map, or whatever
              }

              or

              @Factory("myMap")
              public Map<String, String> initMyMap(){
                  // create map as you'd like or simply
                  return new HashMap<String, String>();
              }

              seam will now how to initialize myMap.
              • 4. Re: Map, new-Operator and In
                nimo22

                I have done that also with a Factory all the time (which works), but I wanted to be conform with SEAM which does not allow the new-Operator.

                • 5. Re: Map, new-Operator and In
                  buckmin.erdem.agaoglu.gmail.com

                  it only applies to @Name defined javabeans and ejbs. it is not restricted anyway, but if you create them with new-operator, seam is not able to manage them efficiently (you have to @Out them which will probably overwrite any configuration around @Name definition). but since List and Map like objects are not defined anywhere seam provides @Factory and @Unwrap components for that matter. more information.