You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by sorinev <so...@gmail.com> on 2014/07/14 11:05:58 UTC

item.getModelObject() result "cannot be cast to"

I'm using the Wicket Quickstart (latest as of yesterday) as a base and I'm
trying to do a very simple loop through data I've grabbed from a database
(Hibernate, MySQL, no Spring). I'm following the various DataView, ListView,
etc examples to the letter and every time I restart the server and reload
the page, I get:

     /Last cause: [Ljava.lang.Object; cannot be cast to com.cfm.Contact/

This is referring to this line:

     /final Contact thisContact = (Contact)item.getModelObject();/

I have WicketApplication.java, HomePage.html, HomePage.java,
HibernateUtil.java, and Contact.java all under /src/main/java/com.cfm

Contact is declared as 

     /public class Contact implements java.io.Serializable {/

and just has some basic string fields with getters and setters.

The relevant part of HomePage.java is:

/        List<Contact> contactList = session.createSQLQuery("select * from
wicket.contacts").list();
        final DataView dv = new DataView("AllContacts", new
ListDataProvider(contactList)){
            public void populateItem(final Item item){
                final Contact thisContact = (Contact)item.getModelObject();
                item.add(new Label("Id", thisContact.getId()));
            }
        };

        add(dv);
/
and it dies at the final Contact thisContact.... line. I have no idea what's
going on. All of the examples I've found through Google all show it
happening in this exact way. I've tried about three different methods of
looping through the data (it's really hard to find how to loop through
database data in wicket...) and it all comes to the same result (even if I
leave the /(Contact)/ part off). 

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/item-getModelObject-result-cannot-be-cast-to-tp4666598.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: item.getModelObject() result "cannot be cast to"

Posted by sorinev <so...@gmail.com>.
I found a way to do it. I just had to change the line from 

/List<Contact> contactList = session.createSQLQuery("select * from
wicket.contacts").list();/

to

/List<Contact> contactList = session.createSQLQuery("select * from
wicket.contacts").addEntity(Contact.class).list();/

meaning the only thing that changed was adding addEntity(Contact.class) in
between the query call and the list call. Thanks for your clear and speedy
replies.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/item-getModelObject-result-cannot-be-cast-to-tp4666598p4666604.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: item.getModelObject() result "cannot be cast to"

Posted by Martin Grigorov <mg...@apache.org>.
On Mon, Jul 14, 2014 at 12:53 PM, sorinev <so...@gmail.com> wrote:

> Ok, that was simple enough (running in Jetty for debug purposes). It shows
> the objects as:
>
> /contactList = {java.util.ArrayList@3397} size = 4/
>
> and then each element in the list is shown as
>
> /java.lang.Object/
>
> So I guess I need to get some converting going on for the list and/or it's
> elements. How do I go about doing that?
>

Consult with your Hibernate books ;-)

First thing I'd try is to use the API with Class as second parameter,
something like: query("select * from blah", Contact.class)


>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/item-getModelObject-result-cannot-be-cast-to-tp4666598p4666602.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: item.getModelObject() result "cannot be cast to"

Posted by sorinev <so...@gmail.com>.
Ok, that was simple enough (running in Jetty for debug purposes). It shows
the objects as:

/contactList = {java.util.ArrayList@3397} size = 4/

and then each element in the list is shown as

/java.lang.Object/

So I guess I need to get some converting going on for the list and/or it's
elements. How do I go about doing that? 

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/item-getModelObject-result-cannot-be-cast-to-tp4666598p4666602.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: item.getModelObject() result "cannot be cast to"

Posted by Martin Grigorov <mg...@apache.org>.
Wicket Quickstart comes with embedded Jetty for much easier development.
In IDEA open class Start.java (it is in src/test/java/...), right click
somewhere in the body of the class and choose "Debug Start.main()" from the
context menu.
Consult with http://www.jetbrains.com/idea/webhelp/debugging.html for more
info.

Another option is to add: System.err.println("RESULT: " + contactList) (as
you'd do with any other language/environment)


Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov


On Mon, Jul 14, 2014 at 12:35 PM, sorinev <so...@gmail.com> wrote:

> I'm not even sure HOW to get into a debuggable environment. The wicket
> framework and whole environment is so bizzare and unfamiliar, i'm not sure
> how (it took me 2 days to get the environment set up to even begin
> programming). I current develop it in IntelliJ IDEA. When I'm ready, I hit
> rebuild project. I then open a terminal (Kubuntu 14.04) in the root of the
> project folder and run it by typing /mvn tomcat:run/. In this case, how
> would I get debug going?
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/item-getModelObject-result-cannot-be-cast-to-tp4666598p4666600.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: item.getModelObject() result "cannot be cast to"

Posted by sorinev <so...@gmail.com>.
I'm not even sure HOW to get into a debuggable environment. The wicket
framework and whole environment is so bizzare and unfamiliar, i'm not sure
how (it took me 2 days to get the environment set up to even begin
programming). I current develop it in IntelliJ IDEA. When I'm ready, I hit
rebuild project. I then open a terminal (Kubuntu 14.04) in the root of the
project folder and run it by typing /mvn tomcat:run/. In this case, how
would I get debug going? 

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/item-getModelObject-result-cannot-be-cast-to-tp4666598p4666600.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: item.getModelObject() result "cannot be cast to"

Posted by Martin Grigorov <mg...@apache.org>.
Hi,


Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov


On Mon, Jul 14, 2014 at 12:05 PM, sorinev <so...@gmail.com> wrote:

> I'm using the Wicket Quickstart (latest as of yesterday) as a base and I'm
> trying to do a very simple loop through data I've grabbed from a database
> (Hibernate, MySQL, no Spring). I'm following the various DataView,
> ListView,
> etc examples to the letter and every time I restart the server and reload
> the page, I get:
>
>      /Last cause: [Ljava.lang.Object; cannot be cast to com.cfm.Contact/
>
> This is referring to this line:
>
>      /final Contact thisContact = (Contact)item.getModelObject();/
>
> I have WicketApplication.java, HomePage.html, HomePage.java,
> HibernateUtil.java, and Contact.java all under /src/main/java/com.cfm
>
> Contact is declared as
>
>      /public class Contact implements java.io.Serializable {/
>
> and just has some basic string fields with getters and setters.
>
> The relevant part of HomePage.java is:
>
> /        List<Contact> contactList = session.createSQLQuery("select * from
> wicket.contacts").list();
>

put a breakpoint and make sure contactList is really a List of Contact
objects


>         final DataView dv = new DataView("AllContacts", new
> ListDataProvider(contactList)){
>             public void populateItem(final Item item){
>                 final Contact thisContact = (Contact)item.getModelObject();
>                 item.add(new Label("Id", thisContact.getId()));
>             }
>         };
>
>         add(dv);
> /
> and it dies at the final Contact thisContact.... line. I have no idea
> what's
> going on. All of the examples I've found through Google all show it
> happening in this exact way. I've tried about three different methods of
> looping through the data (it's really hard to find how to loop through
> database data in wicket...) and it all comes to the same result (even if I
> leave the /(Contact)/ part off).
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/item-getModelObject-result-cannot-be-cast-to-tp4666598.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>