1 2 3 Previous Next 38 Replies Latest reply on Mar 24, 2011 7:56 AM by waltc

    java.util.Vector cannot be cast to javax.faces.model.DataModel

    waltc
      I have the following code:


      `@Name("DailyActivity")
      public class DailyActivityBean implements DailyActivity {
              @DataModel
              protected List<ActivityDay> dayList;


              @Factory("dayList")
              public List<ActivityDay> createDayList() {
                      List<ActivityDay> dayList = new Vector<ActivityDay>();
                      buildDataList();  // build tree keyed on date with value of ActivityDay object
                      for (String day : dayBean.keySet()) {
                              ActivityDay aDay = dayBean.get(day);
                              dayList.add(aDay);
                      }
                      statusMessages.add(Severity.INFO, "created " + dayList.size() + " day elements");
                      return dayList;
              }
      `




      `           <f:facet name="label">
                  <h:panelGroup><h:graphicImage style="vertical-align: middle; padding-right: 4px;"/>Grid (#{dayList.size()})</h:panelGroup>
              </f:facet>
              <div class="results">
                              <rich:dataTable value="#{dayList}" var="_day"
                         columnClasses="left, right"
      `

      From the writeup in Dan's book I should be using Factory rather than outjecting. Also, in his example of 'newGolfers' the @Factory method was a void. I patterned my code after his. This worked fine, a void return, except the dayList returned was always empty, even though the statusMessage said it added 262 entries.
      According to the text of Dan's book:

      The first time the #{newGolfers} value expression is encountered in the JSF view, the
      newGolfers context variable is uninitialized. Seam will turn to the @Factory method D
      to resolve a value. Before the method proceeds the EntityManager property B is
      injected. The method then uses the EntityManager to query the database for the newest
      golfers E, shuffles the result, and pares it down to the configured display size F. Out-
      jection is then applied, which initializes the newGolfers context variable as a JSF Data-
      Model C. Seam then returns control to the view renderer, which uses the newly formed
      data model to render the <rich:dataList>.

      This doesn't appear to have happened. When I made the @Factory return the List<ActivityDay> the datagrid was automatically populated, the first time. If I depress display button I get the exception of classcastexception saying the List<ActivityDay> cannot be cast to DataModel.



      `<h:form styleClass="edit">
      <rich:simpleTogglePanel>
              <f:facet name="header">Activity Filter</f:facet>
              <h:panelGrid columns="4" width="20%" height="20%">
              <rich:column width="20%">Engine Type
                      <s:decorate template="layout/display.xhtml">
                              <rich:comboBox id="compositeActivityEngineType" value="#{compositeActivityList.engineType}" abel="Engine" required="true" selectFirstOnUpdate="true">
                              </rich:comboBox>
                      </s:decorate>
              </rich:column>
              <rich:column width="20%">Country
              <s:decorate template="layout/display.xhtml">
                      <rich:comboBox id="compositeActivityCountry" suggestionValues="#{DailyActivity.engines}"  value="#{compositeActivityList.country}" label="Country" required="false">
                      </rich:comboBox>
              </s:decorate>    
              </rich:column>
              <rich:column width="20%">Document Type
              <s:decorate template="layout/display.xhtml">
                      <rich:comboBox id="compositeActivityDocType" suggestionValues="#{DailyActivity.docs}" directInputSuggestions="true" label="Document" required="true" value="#{compositeActivityList.docType}" selectFirstOnUpdate="true">
                      </rich:comboBox>
              </s:decorate>
              </rich:column>
              <rich:column width="20%">Smoothing factor
              <s:decorate template="layout/display.xhtml">
              <rich:inputNumberSpinner id="smoothingSpinner" label="Smoothing Period" maxValue="21" minValue="1" step="1" value="#{DailyActivity.smoothing}">          
              </rich:inputNumberSpinner>
              </s:decorate>
              </rich:column>
              </h:panelGrid>
             
          <div class="actionButtons">
              <h:commandButton id="Display" value="Display"  rerender="output"/>
              <s:button id="reset" value="Reset" includePageParams="false"/>
          </div>

      </rich:simpleTogglePanel>
      </h:form>

      `

      What is really interesting is the exception stacktrace shows:


      `Caused by java.lang.ClassCastException with message: "java.util.Vector cannot be cast to javax.faces.model.DataModel"

      org.jboss.seam.databinding.DataModelBinder.getSelection(DataModelBinder.java:14)
      org.jboss.seam.Component.injectDataModelSelection(Component.java:1638)
      org.jboss.seam.Component.injectDataModelSelections(Component.java:1619)
      org.jboss.seam.Component.inject(Component.java:1558)
      org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:61)
      org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
      org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
      org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
      org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
      com.monster.analytics.action.DailyActivityBean_$$_javassist_seam_3.getSmoothing(DailyActivityBean_$$_javassist_seam_3.java)
      `



        • 1. Re: java.util.Vector cannot be cast to javax.faces.model.DataModel
          waltc

          To highlight: What I do not understand is why in the stack trace, for instance, above, the root of the problem is something other than the @Factory method createDayList. I originally had the list be an ArrayList and the error message was ArrayList could not be converted. In the above case I simply removed the smoothing control and it started to work albeit the controls in the form appeared to have no effect whatsoever.


          Whatever advice you have would be appreciated!


          TIA,


          Walt

          • 2. Re: java.util.Vector cannot be cast to javax.faces.model.DataModel
            waltc

            Sorry...this is Seam 2.2.1Final

            • 3. Re: java.util.Vector cannot be cast to javax.faces.model.DataModel
              kragoth

              I'm no expert on the @Factory side of things but a quick google showed an interesting pattern. If you can't get the void @Factory pattern working and have resorted to a return List<?> pattern then the pattern seems to indicate you should then remove your @DataModel variable.


              So, just this should be enough.


              @Name("DailyActivity")
              public class DailyActivityBean implements DailyActivity {
                      @Factory("dayList")
                      public List<ActivityDay> createDayList() {
                              List<ActivityDay> dayList = new Vector<ActivityDay>();
                              buildDataList(); // build tree keyed on date with value of ActivityDay object
                              for (String day : dayBean.keySet()) {
                                      ActivityDay aDay = dayBean.get(day);
                                      dayList.add(aDay);
                              }
                              statusMessages.add(Severity.INFO, "created " + dayList.size() + " day elements");
                              return dayList;
                      }
              



              The lines below have been removed.


              @DataModel
              protected List<ActivityDay> dayList;
              



              Why are you using Vector anyway?

              • 4. Re: java.util.Vector cannot be cast to javax.faces.model.DataModel
                waltc

                Thanks, I will try that as well. The reason I went to using Vector, which I supremely do not like (forgot why now) was ArrayList didn't work and I thought something simpler would maybe work. One thing I've noticed about Seam (and was told to me on my first or second forum question, was that, and I am paraphrasing, 'squirrelly'. In that case I started talking about where getSmoothing() was in the stack trace I decided maybe that had nothing to do with vector (it used to say ArrayList) and more to do with the number control so I cut it out entirely. The exception stopped but it still didn't work.  I looked at some apps I wrote in Seam and I have factories that the method returns null that work fine. I have filters in subpanels that work fine, I have charts, in the other tab, that work fine. No, I do not want to co-mingle problems in one thread, hoping I have better luck with people responding. Thank you, btw. It's just that things that should work, don't seem to. I can not see anything structurally different between this app and one that works...except this is 2.2.1 and the other was 2.0 or 2.1. I set this up in its own eclipse workspace, a brand new seam-gen project

                • 5. Re: java.util.Vector cannot be cast to javax.faces.model.DataModel
                  kragoth

                  After spending roughly 3 years now working with Seam I've learned quite a lot about Seam. Mostly, I've learned that there are a lot of things that are not always 'quite' what they seem to be. I started the app with 0 Seam experience and our design shows that. If I had the experience back then like I do now there would be a number of things that I would do differently.


                  On the surface Seam is a fantastic framework that allows you to build a quite reasonable prototype faster then anything else around. But, once you go below that surface the learning curve puts Mt Everest to shame. Of course I'm slightly exaggerating the situation but, I think the point still stands. Seam is a large, complex and powerful framework which tries harder then most other frameworks to be easy to use and relatively straight forward. But, there are/always will be some quirks. These quirks can actually drive one to sheer frustration as you stare at code that should just work.


                  So, now with all that I've done if I was starting from scratch again I would actually try to remove as much reliance on Seam's @Create, @Factory, @DataModel @Out or any other similar annotation as I could. I would use Seam beans for application logic which would then populate a Seam bean that contains data. This data bean would be annotated with @BypassInterceptors. My xhtml pages would bind all value attributes to some value in these data beans and action/actionListeners would be bound to my application logic beans. I would do this only because 5% of the time I spend hours...sometimes days trying to work out what combination of the previously mentioned annotations/ideas must be in place in order to solve my problem.


                  Don't get me wrong. I like Seam. I actually like the ideas behind those annotations. I like the idea of the famework. (Although I'm not convinced about the extended persistence concept just yet). And for the most part I think Seam has done a rather good job of providing a nice way to use JSF. Except for the horrible navigation framework that it insists on using...ugh. (Search for my discussions I had with someone else about NavigationManager to see how I do navigation in my app. This is one thing I'm proud of in my app :P)


                  But, problems like yours can really hurt people really want to just get something working and not fuss around.


                  However, back to your problem. ArrayList is not the problem, so switch back to it :).


                  Let me know how you go once you remove that @DataModel annotation.

                  • 6. Re: java.util.Vector cannot be cast to javax.faces.model.DataModel
                    waltc

                    I will do that first thing in the morning. What do you think about Grails? I've been working with Sean also for about 3 years, starting also at about 0. I had some jsp, some servlet, a little bit of Struts, but when the whole web thing took off mid to late 90's I wanted nothing to do with html and tables within tables within tables. Now web front ends are clearly the future. But in the last 3 years I have used Seam for mostly internal things, analytical analysis, not full time. I think to really understand something you have to immerse yourself in it. For this project here, short time frame I thought maybe perhaps Grails might be way more light weight. After a few days I decided I know more about or maybe just Seam was better. Jury is still out but I pretty much have to dance with the one who brought me. More tomorrow and thanks again.

                    • 7. Re: java.util.Vector cannot be cast to javax.faces.model.DataModel
                      kragoth

                      To be honest I have had very little real experience with anything outside of the Seam/JSF world. I started working on this particular project about a year after finishing my degree. So sure, I've played around with jsp, asp, vb, and a couple of other things. But, not to the extent where I have any in depth knowledge. So, ultimately, you are probably a much more experienced developer then me.


                      However, on this project I was thrown in the deep end as the Seam 'expert' we had wasn't really an 'expert' and left early on in the project anyway. So, I put my hand up (was encouraged to :P) to learn the Seam/JSF/front end of our app and tackle any problems in that layer. I've spent many many hours now stepping through the Seam source and learning how it does things. Once I understood what was going on inside Seam it made solving some of our problems a lot easier.


                      So, as far as Grails goes...I really couldn't say if it is better or worse. There's pros and cons all around but, I think choosing the one that you were more familiar with was probably the right way to go.


                      The tables in tables in tables comment brought back a very big debate I with a friend. Were div layouts better then table layouts. I argued tables, he argued divs. To this day, I will take a table layout over a div layout any day of the week. ;) I do think however that the web interface craze has swung too far. Now apps that should have been developed with a rich client are being forced into this whole 'Let's have a web front end'. But, hey I'm ranting now and going way off topic. :)


                      Hear from ya tomorrow then.

                      • 8. Re: java.util.Vector cannot be cast to javax.faces.model.DataModel
                        waltc

                        Hi Tim,
                          This is going from bad to worse. Something else that may be relevant is I am trying using output of 'war' vs 'ear'. In my early days I tried to produce a war artifact. While the .war was produced they didn't work correctly so once I switched to ear they worked way more predictably. So when I scratched my head about why this wasn't working when I looked at a previous Seam project I did that worked fine and there were no appreciable differences in the xhtml or action beans, it became 'magic'. This version didn't work because it didn't feel like working. Kind of like the stack trace of can not convert ArrayList to DataModel (when the book clearly says an ArrayList converts to an ArrayDataModel. The book, and my previous projects have @Factory of type null. WTF! It hit me like a ton of bricks about 5 mins ago. All the others were ear projects. Does this ring a bell in your experience with this?


                        So no I made your suggested changes, which kind of work, which is to say it doesn't behave more wrong but here is what I was seeing yesterday and today. I have a form to set the restrictions on the db query. I put in a series of statusMessages (debugging with printf) and I see the form gets processed AFTER the page gets refreshed. Oh, also, in the constructor for the ActivityList object I primed the restrictions so I'd always get a homogenious subset, not heterogenious universe. I fire up the initial page and the selection filters properly show the primed state of the restrictions as does the data reflect that. I change the filter properties and hit display, the filters change back to the primed values and the display remains unchanged (it simply does the same initial select where). In the statusMessage section it clearly shows the restrictions being set after the grid is built. That is so counter intuitive. Any wisdm on forms? Oh, that used to work (I think). It atleast took the changes but I wasn't sure if what got displayed wasn't the last change I had made not the current one, if you know what I mean. Here is the form:



                        <h:form styleClass="edit">
                        <rich:simpleTogglePanel  switchType="ajax" >
                                <f:facet name="header">Activity Filter</f:facet>
                                <h:panelGrid columns="4" width="20%" height="20%">
                                <rich:column width="20%">Engine Type
                                        <s:decorate template="layout/display.xhtml">
                                                <rich:comboBox id="compositeActivityEngineType" value="#{compositeActivityList.engineType}" label="Engine" required="true" selectFirstOnUpdate="true">
                                           <f:selectItem itemValue="new age"/>
                                            <f:selectItem itemValue="classic"/>
                                                </rich:comboBox>
                                        </s:decorate>
                                </rich:column>
                                <rich:column width="20%">Country
                                <s:decorate template="layout/display.xhtml">
                                        <rich:comboBox id="compositeActivityCountry" value="#{compositeActivityList.country}" label="Country" required="false">
                                   <f:selectItem itemValue="EN"/>
                                   <f:selectItem itemValue="FR"/>
                                   <f:selectItem itemValue="DE"/>
                                        </rich:comboBox>
                                </s:decorate>     
                                </rich:column>
                                <rich:column width="20%">Document Type
                                <s:decorate template="layout/display.xhtml">
                                        <rich:comboBox id="compositeActivityDocType" directInputSuggestions="true" label="Document" required="true" value="#{compositeActivityList.docType}" selectFirstOnUpdate="true">
                                   <f:selectItem itemValue="credits"/>
                                   <f:selectItem itemValue="debits"/>
                                        </rich:comboBox>
                                </s:decorate>
                                </rich:column>
                                </h:panelGrid>
                                
                            <div class="actionButtons">
                                <h:commandButton id="Display" value="Display"  action="#{DailyActivity.createDayList()}"/>
                                <s:button id="reset" value="Reset" includePageParams="false"/>
                            </div>
                        
                        </rich:simpleTogglePanel>
                        </h:form>
                        



                        • 9. Re: java.util.Vector cannot be cast to javax.faces.model.DataModel
                          waltc

                          In looking at it in that window I noticed something I really didn't catch in Eclipse, the layout was display, and maybe it should be edit??? Works better now and it appears to be updating the restrictions before building the list, or maybe it just changed its mind ... again.


                          What do you know about charts?

                          • 10. Re: java.util.Vector cannot be cast to javax.faces.model.DataModel
                            waltc

                            I thought, for the purposes of another set of eyes, I'd include the xhtml.



                                 <rich:tab>
                                     <f:facet name="charts">
                                         <h:panelGroup><h:graphicImage style="vertical-align: middle; padding-right: 4px;"/>Chart (#{DailyActivity.resultList.size()})</h:panelGroup>
                                    </f:facet>
                                    <div class="results">
                                    <p:linechart title="Activity" domainAxisLabel="Date" domainLabelPosition="UP_45" legend="true" >
                                         <p:series key="inserts">
                                             <ui:repeat value="#{DailyActivity.resultList}" var="_day">
                                                   <p:data key="#{_day.date}" value="#{_day.dayTotal.raw.inserts}"/>                           
                                             </ui:repeat>
                                        </p:series>
                                         <p:series key="deletes">
                                               <ui:repeat value="#{DailyActivity.resultList}" var="_day">
                                                  <p:data key="#{_day.date}" value="#{_day.dayTotal.raw.deletes}"/>                           
                                               </ui:repeat>
                                         </p:series>
                                 
                                         <p:series key="smoothed Inserts">
                                              <ui:repeat value="#{DailyActivity.resultList}" var="_day">
                                                  <p:data key="#{_day.date}" value="#{_day.dayTotal.rolling.inserts}"/>                           
                                               </ui:repeat>
                                         </p:series>
                                 
                                         <p:series key="smoothed Deletes">
                                              <ui:repeat value="#{DailyActivity.resultList}" var="_day">
                                                  <p:data key="#{_day.date}" value="#{_day.dayTotal.rolling.deletes}"/>                           
                                              </ui:repeat>
                                         </p:series>
                                </p:linechart>
                                </div>
                                 </rich:tab>
                            


                            The thing about the charting is I don't even get an empty box, nothing is rendered. Is there an issue with charting in 2.2.1?

                            • 11. Re: java.util.Vector cannot be cast to javax.faces.model.DataModel
                              kragoth

                              So, after reading all that I'm a little unsure....Is your first problem fixed now? (The @Factory/@DataModel) issue.


                              As far as .ear vs .war I've never packaged any of my apps as .ear. I've always used .war so have not experienced any of the pain associated with going from one to the other. My guess is that you're just missing something rather simple in the transition and you'll probly kick yourself when you see it. :) But, it's really hard to know exactly what to look for. If I get any ideas I'll put them here.


                              Now...charting. Hehe, you are bringing out a lot of things I haven't played around with. But, my golden rule is that when experiencing problems like this Remove everything that doesn't relate to my problem. For instance you may find out that the problem has nothing to do with the chart but the compatibility of the rich:tab and the p:linechart. So, take away everything else on the page except the chart and see if it draws. If it doesn't go get an example that does work and put it on your page...etc. Once you get something that works build back up to your solution and find where it breaks.


                              I wish I could be of more help. :S


                              I may have some time today to play around with the p:linechart tag as I'm in a bit of a code lockdown before release atm. See how I go.

                              • 12. Re: java.util.Vector cannot be cast to javax.faces.model.DataModel
                                kragoth

                                Just so I'm on the right page here....


                                the p:linechart tag you are using is that the Seam linechart of the primefaces linechart?


                                If it's Seam's then....I was under the impression that that tag was only used inside of the p:document tag. That being that the tag is part of Seam's pdf tag lib and wont generate useful html.


                                If it is primefaces then....I'm not sure if I'm going to be able to get that working in my app to take a look.

                                • 13. Re: java.util.Vector cannot be cast to javax.faces.model.DataModel
                                  waltc

                                  Yes, The other problem is resolved. Now I have the charting issue.


                                  Problem 1) mine The namespace was messaged up for the p:
                                  Now it fails for an invalid index.  The other charts I've done with seam, about 18-24 months ago, I had this problem then too. I can not find that forum thread. Oh, it is not primefaces, it is richfaces.


                                  I do have another question for you though. My main panel is a tabpanel with two tabs.


                                  One tab is grid (250)
                                  The other is chart(250)


                                  the 250 is the count of the number of rows in the ArrayList. When I switch threads the count goes to (), same bean, it should still be there, why does it lose reference?

                                  • 14. Re: java.util.Vector cannot be cast to javax.faces.model.DataModel
                                    kragoth

                                    Yes, The other problem is resolved. Now I have the charting issue.

                                    fantastic :)



                                    Problem 1) mine The namespace was messaged up for the p:
                                    Now it fails for an invalid index.  The other charts I've done with seam, about 18-24 months ago, I had this problem then too. I can not find that forum thread. Oh, it is not primefaces, it is richfaces.

                                    OK....I've never seen a richfaces chart tag. Do you have a link I could read about it? After doing a quick google I still can't actually find a chart tag belonging to the richfaces lib.



                                    I do have another question for you though. My main panel is a tabpanel with two tabs.

                                    One tab is grid (250)
                                    The other is chart(250)

                                    the 250 is the count of the number of rows in the ArrayList. When I switch threads the count goes to (), same bean, it should still be there, why does it lose reference?


                                    What do you mean....'When I switch threads'? Do you mean when you switch panels?

                                    1 2 3 Previous Next