This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes GWT and the fly likes List does not contain objects added in anonymous inner class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » GWT
Bookmark "List does not contain objects added in anonymous inner class." Watch "List does not contain objects added in anonymous inner class." New topic
Author

List does not contain objects added in anonymous inner class.

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Hi all, I am having an anonymous inner class in which I am adding objects to a list which is declared outside the inner class. After the control comes out of the inner class the list size is again zero. Can anyone explain why is this so?


Love all, trust a few, do wrong to none.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3419
    
  12
Have you checked that your onSuccess method is actually being called ?
If it is, have you checked that the list that is passed to it actually contains any objects ?
Your onFailure method is empty ? How do you know if an exception is being thrown or not ?


Joanne
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Joanne Neal wrote:Have you checked that your onSuccess method is actually being called ?
If it is, have you checked that the list that is passed to it actually contains any objects ?
Your onFailure method is empty ? How do you know if an exception is being thrown or not ?
I used print statements, success method is being called and the list contents are printed in my console.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

That's a GWT asynchronous callback. The "asynchronous" part here is key - it means that the code in the onSuccess / onFailure methods will be executed at some unspecified time in the future.

What happens with that code is as follows:
1) The contacts List is created.
2) An HTTP call is made to the listContacts method of the rpcService.
3a) The contacts list is printed.
4a) The contacts list is returned.

3b) The rpcService call returns a List<Contact>.
4b) The contacts list is filled with the contents of this List.

The problem is, you don't know when steps 3b and 4b occur. They could occur almost immediately, or they could occur a minute later if the network latency is high.

So unfortunately, your design will not work the way you want it. That's something you will run into not just with GWT but with all frameworks that use asynchronous callbacks (like ActionListeners in AWT / Swing).


I had to deal with the same issue, and I solved it by adding another callback. This callback was provided by the code that called the method, and the asynchronous method simply calls the callback method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Rob Spoor wrote:That's a GWT asynchronous callback. The "asynchronous" part here is key -
Oops!!! I missed it. I am sorry for being a stupid, the name itself says its an asynchronous call. I could not notice it. Thank you Rob.
 
 
subject: List does not contain objects added in anonymous inner class.
 
Similar Threads
Cell Widget Table in GWT 2.1.0 with expanded/collapsed rows
Serching for a surname in my collection
Caught an exception while evaluating expression '....size()==0' against value stack
Error in the java language?
TreeMap equals check